簡體   English   中英

從AppDelegate和Tab Bar推送View Controller

[英]Push View Controller From AppDelegate and Tab Bar

我的應用程序使用Tab Bar Controller作為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 關於我還能做什么的任何建議?

selectedViewController的返回類型是UIViewController,因此您需要告訴編譯器它實際上是一個導航控制器。 你用演員表演,

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

嘗試這個!!

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

如果這是你期待的那個,請在這里評論!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM