Zoro
Zoro

Reputation: 51

Cannot push viewcontroller from appdelegate

  1. In my window base application I need to navigate to editorview from my appdelegate when i click on the second item of my tabbarcontroller it will shows an actionsheet with three option.

  2. actionsheet works fine, it will shows an actionsheet if you choose the second item from tabbar.

  3. But i need to push to the other view when i choose the first option of my action sheet

  4. i declare my actionsheet in appdelegate.

  5. i already implement the actionsheetdelegate

    • (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

the method is trigerred and it can shows if i do nslog. but it cannot push to the viewcontroller that i want.. here's my code:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 0)
        {
            DetailViewController *v2 = [[DetailViewController alloc] init];

            NSLog(@"PUSH TRIGER");

            [self.window.rootViewController.navigationController pushViewController:v2 animated:YES];
        }
    }

NOTE: I already embed my tabbarcontroller and navigation controller in my storyboard

Upvotes: 1

Views: 6891

Answers (3)

Alex Moleiro
Alex Moleiro

Reputation: 1164

It is full of bad answer related to this issue.

I hope mine fits your requirements.

1.- Inside of

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

// 1.- First: instantiate the navigationController. Normally the rootviewcontroller

 UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

// 2.- Second: Instantiate Storyboard // It might be tagged MainStoryBoard. Be careful if you've changed it

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];

// 3.- Third instantiate the VC you want to navigate // In my particular case it is called IniciarSliderViewController . Don't forget to import it in the appdelegate. Also pay attention to tagged correctly. In my case MenuSlider

IniciarSliderViewController *controller = (IniciarSliderViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"MenuSlider"];

//4.- And last one, you can now push as you usually do in the VC

[navigationController pushViewController:controller animated:YES];

Don't hesitate to ask if you still have problems

Upvotes: 10

Alphapico
Alphapico

Reputation: 3043

Do like this

What did you assign VC for UITabBarController ? I bet UIViewController? Try assign UINavigationController instead of UIViewController

synthesize UINavigationController *navController;

self.navController = [[UINavigationController alloc] init];
SomeViewController *viewController = [[SomeViewController alloc]  initWithNibName:@"SomeViewController" bundle:nil];
self.navController.viewControllers = [NSArray arrayWithObject:viewController];

UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:
self.navController, nil]];

Then on your actionsheet

[self.navController pushViewController:v2 animated:YES];

EDIT for storyboard

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
   self.navController = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:@"MyNavController"];

Hope that helps

Upvotes: 2

Gaurav Patel
Gaurav Patel

Reputation: 957

instead of this self.window.rootViewController.navigationController

use UINavigationController object

Upvotes: -2

Related Questions