Search code examples
iosuiactionsheetpushviewcontrollerappdelegate

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


Solution

  • 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