从AppDelegate和选项卡栏推送视图控制器

3
我的应用程序是以Tab Bar控制器作为RootViewController来设置的,每个Tab都有一个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。你有什么其他的建议吗?针对这个问题,我建议您检查一下是否正确导入了相关的库文件,并且确认您正在使用的是正确的视图控制器类。

你能发布完整的警告信息和一部分代码吗? - rishi
@rishi 请查看已编辑的问题。 - user717452
我认为你需要这样做 - [self.tabBarController.selectedViewController.navigationController pushViewController:self.newView animated:YES]; - rishi
2个回答

10

selectedViewController方法的返回类型是UIViewController,所以你需要告诉编译器它实际上是一个导航控制器。你可以通过强制转换来实现这一点,

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

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

如果这是你期待的,请在此处发表评论!


我不使用Storyboard来开发我的应用,只用XIB。 - user717452
ViewControllerOne* tab1 = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]]; 也要对ViewControllerTwo进行编辑,并删除UIStoryBoard。 - Sandeep
我想要选项卡栏的点击事件,如果在iOS中没有访问相机的权限,则不打开视图控制器。 - Alok

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接