从AppDelegate和Tab Bar推送View Controller

时间:2014-06-24 04:26:05

标签: ios uinavigationcontroller uitabbarcontroller appdelegate pushviewcontroller

我的应用程序使用标签栏控制器设置为RootViewController,每个标签中都有一个NavigationController。执行某些操作时,我希望应用将ViewController推送到屏幕上。这个流程是当应用程序启动或从后台打开时,它会检查存储的NSDate并将其与当前日期进行比较。如果符合正确的条件,则会显示UIAlertView。如果按钮我命名为" Push"选中后,它会运行代码来推送新视图。这就是我需要从AppDelegate运行它的原因,因为如果在后台使用该应用程序,则无法保证可以打开哪个选项卡。由于每个标签都包含一个NavigationController,我想我可以从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];
         }
     }
}

我收到一条警告信息UIViewController may not respond to -pushViewController:animated。关于我还能做什么的任何建议?

2 个答案:

答案 0 :(得分:10)

selectedViewController的返回类型是UIViewController,因此您需要告诉编译器它实际上是一个导航控制器。你用演员表来做到这一点,

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

答案 1 :(得分:0)

试试这个!!

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

如果这是你期待的那个,请在这里评论!!