【问题标题】:Push View Controller From AppDelegate and Tab Bar从 AppDelegate 和标签栏推送视图控制器
【发布时间】:2014-06-24 04:26:04
【问题描述】:

我的应用设置了一个标签栏控制器作为RootViewController,每个标签都有一个NavigationController。当执行某些操作时,我希望应用程序将ViewController 推送到屏幕上。其流程是,当应用程序启动或从后台打开时,它会检查存储的 NSDate 并将其与当前日期进行比较。如果满足正确的条件,则会显示UIAlertView。如果选择了我命名为“推送”的按钮,它会运行代码来推送新视图。这就是我需要从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 查看已编辑的问题
  • 我认为你必须这样做 -[self.tabBarController.selectedViewController.navigationController pushViewController:self.newView animated:YES];

标签: ios uinavigationcontroller uitabbarcontroller appdelegate pushviewcontroller


【解决方案1】:

selectedViewController 的返回类型是 UIViewController,所以你需要告诉编译器它实际上是一个导航控制器。你可以通过演员来做到这一点,

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

【讨论】:

    【解决方案2】:

    试试这个!!

    - (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
    • ViewControllerOne* tab1 = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil]];也为 ViewControllerTwo 编辑它并删除 UIStoryBoard
    • 我想要标签栏的点击事件,如果你在IOS中无权访问相机,就不要打开视图控制器。
    猜你喜欢
    • 2015-05-23
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多