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

标签 iphone ios objective-c

我正在尝试重写应用程序的 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/

相关文章:

iphone - UITableView:以编程方式设置数据源

iphone - 如何实现带有行和列的表格

ios - 如何从 Swift 中的原始值获取枚举?

ios - WidgetKit自定义Intent参数选择

ios - 自动布局单元格高度不适用于单个单元格

objective-c - 具有非 ARC 静态库的启用 ARC 的项目

ios - 用于 App Store 显示的 CFBundleDevelopmentRegion (info.plist) 的字符串格式

iphone - PVR 纹理工具构建阶段

ios - 带有单元格 subview 的 UITableView 滚动性能不佳。

iphone - 如何获得警报信息?