Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pushViewController does not work with TabbarController

I have a Tab Bar Controller app which using storyboard. Each tab has a UIWebview. I want to capture link when the user click the link on Webview if the link is an external link (if it is not my site) I want to open the link in a different UIViewControl. (like a popupi Twitter iphone app do that)

Right now; I got this code for capture the link (if the link is bing.com then it should open it another view controller) but I cannot open another UIViewController (in this case PopViewController). It gives me this error:

No visible @interface for 'GundemViewController' declares the selector 'pushViewController:animated:'

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

//CAPTURE USER LINK-CLICK.

NSURL *url = [request URL];

NSString *urlString = [url absoluteString];

/******
 UIWebViewNavigationTypeLinkClicked: When user click on a link in the app it senses the action
 */

if (navigationType == UIWebViewNavigationTypeLinkClicked) {
    if ([urlString hasPrefix:@"http://www.bing.com/"]) {

        PopViewController *popUpView = [[PopViewController alloc] initWithNibName:@"PopupViewController" bundle:nil];

        [self pushViewController:popUpView animated:YES];
        return NO;
    }
}


return YES;
}
like image 210
Umut Koseali Avatar asked Apr 06 '13 15:04

Umut Koseali


2 Answers

You can embed navigation controllers in each tab, so that instead of:

UITabBarController
    Tab1 View Controller
    Tab2 View Controller
    Tab3 View Controller

you have

UITabBarController
    Tab1 Navigation Controller
        Tab1 View Controller
    Tab2 Navigation Controller
        Tab2 View Controller
    Tab3 Navigation Controller
        Tab3 View Controller

and so on. You can only call -pushViewController:animated: on an instance of a navigation controller.

In your app delegate, you probably initialize each of your view controllers, put them in an array, and then assign that array to the tab bar controller. To add navigation controllers, you have an extra step:

UIViewController *tab1Controller = [[MyViewController alloc] initWithNibNamed:@"MyViewController" bundle:nil];
UINavigationController *tab1Nav = [[UINavigationController alloc] initWithRootViewController:tab1Controller];

// etc

Then set the array of view controllers for your tab bar controller to the navigation controllers you've created.

In your view controllers, rather than call -pushViewController:animated: on self, you'll call it on self.navigationController.

edit:

If you don't want a navigation controller, you can use

[self presentViewController:popUpView 
                   animated:YES 
                 completion:nil];

to present the pop up. In the pop up, when the user touches the close button, use

[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

to dismiss it.

like image 192
Seamus Campbell Avatar answered Nov 17 '22 20:11

Seamus Campbell


What is GundemViewController? Look into why that is being returned from self.navigationController instead of UINavigationController. One way to do that is to set a breakpoint just before you push the view controller and then step into the self.navigationController call.

like image 41
David Beck Avatar answered Nov 17 '22 18:11

David Beck