简体   繁体   中英

Can't push view controller after overriding openURL

I'm trying to override my application's openURL method to intercept link clicks in a UITextView. The UITextView is in aa navigation based DetailViewController, and when the user clicks the link, I want to push a web view onto the navigation stack.

I verified that my method is being called by logging the intercepted url to the console, but the navigation controller is not pushing my WebViewController at all. I made a button in interface builder and added it onto the same view with the textview just to verify the WebView gets pushed. The button was just for testing purposes.

The problem seems that the navigationController pushViewController code isn't getting fired when I call the method from the AppDelegate, even though the NSLog shows I'm getting the valid intercepted url.

I appreciate any help offered! Code:

Inside 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];
}

I solved the problem by using an NSNotification. Code below for others who find this:

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM