user717452
user717452

Reputation: 111

Push View Controller From AppDelegate and Tab Bar

My app is setup with a Tab Bar Controller as the RootViewController, and each Tab has a NavigationController in it. When certain actions are performed, I want the app to push a ViewController onto the screen. The flow of this is that when the app starts, or opens from background, it checks a stored NSDate and compares it to the current date. If the right condition is met, it shows a UIAlertView. If the button I named "Push" is selected, it runs the code to push the new view. This is the reason I need it to be ran from the AppDelegate, as there is no guarantee what tab may be open if the app is being used in the background. Since every tab contains a NavigationController, I thought I could run this from the AppDelegate:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

      if (alertView.tag == 100) {
         if (buttonIndex == 0) {
              //Cancel
              NSLog(@"Cancel");
          }
         if (buttonIndex == 1) {
              NSLog(@"OK");
              [self.tabBarController.selectedViewController pushViewController:self.newView animated:YES];
         }
     }
}

I get a warning message that says UIViewController may not respond to -pushViewController:animated. Any suggestions as to what else I could do?

Upvotes: 3

Views: 5112

Answers (2)

Sandeep
Sandeep

Reputation: 110

Try this!!

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == 0) {
    NSLog(@"Cancel");
}else{
    NSLog(@"Push");
    [self loadTabbar];
} 
}

-(void)loadTabbar{
UITabBarController *tabbarController = [[UITabBarController alloc]init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

ViewControllerOne *tab1 = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerOne"];
UINavigationController *navi1 = [[UINavigationController alloc]initWithRootViewController:tab1];
tab1.tabBarItem.title = @"Tab1";
tab1.tabBarItem.image = [UIImage imageNamed:@"1.png"];

ViewControllerTwo *tab2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerTwo"];
UINavigationController *navi2 = [[UINavigationController alloc]initWithRootViewController:tab2];
tab2.tabBarItem.title = @"Tab2";
tab2.tabBarItem.image = [UIImage imageNamed:@"2.png"];

NSArray *tabArrays = [[NSArray alloc]initWithObjects:navi1,navi2, nil];

tabbarController.viewControllers = tabArrays;
tabbarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tabbar_selected.png"];

[self.window setRootViewController:tabbarController];
[self.window makeKeyAndVisible];
}

Comment here if this is the one you are looking forward!!

Upvotes: 0

rdelmar
rdelmar

Reputation: 104082

The return type for selectedViewController is UIViewController, so you need to tell the compiler that it's actually a navigation controller. You do that with a cast,

[(UINavigationController *)self.tabBarController.selectedViewController pushViewController:self.newView animated:YES];

Upvotes: 10

Related Questions