gpt4 book ai didi

iphone - 覆盖 openURL 后无法推送 View Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:46:28 26 4
gpt4 key购买 nike

我试图覆盖我的应用程序的 openURL 方法以拦截 UITextView 中的链接点击。 UITextView 位于基于导航的 DetailViewController 中,当用户单击链接时,我想将 Web View 推送到导航堆栈上。

我通过将截获的 url 记录到控制台来验证我的方法正在被调用,但导航 Controller 根本没有推送我的 WebViewController。我在界面生成器中制作了一个按钮,并将其添加到与 TextView 相同的 View 中,只是为了验证 WebView 是否被推送。该按钮仅用于测试目的。

问题似乎是当我从 AppDelegate 调用该方法时,navigationController pushViewController 代码没有被触发,即使 NSLog 显示我正在获取有效的拦截 url。

我感谢提供的任何帮助!代码:

在 AppDelegate.m 中:

- (BOOL)openURL:(NSURL *)url
{
DetailViewController *webView = [[DetailViewController alloc]init];

webView.url = url;

[webView push];

return YES;
}

DetailViewController.h:

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UIGestureRecognizerDelegate>

@property (nonatomic, strong) NSURL *url;

- (void)push;

@end

DetailViewController.m:

- (void)push
{
WebViewController *webView = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];

webView.url = self.url;

NSLog(@"%@",self.url);

[self.navigationController pushViewController:webView animated:YES];
}

最佳答案

我使用 NSNotification 解决了这个问题。以下代码适用于发现此问题的其他人:

AppDelegate.m:

- (BOOL)openURL:(NSURL *)url
{
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"WebViewNotification" object:url]];

return YES;
}

DetailViewController.m:

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(webViewNotification:) name:@"WebViewNotification" object:nil];
}

- (void)webViewNotification:(NSNotification *)notification
{
NSURL *url = [notification object];

WebViewController *webView = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];

webView.url = url;

[self.navigationController pushViewController:webView animated:YES];
}

关于iphone - 覆盖 openURL 后无法推送 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17325676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com