3

我的應用程序設置了一個標籤欄控制器作爲RootViewController,每個標籤有一個NavigationController在其中。當執行某些操作時,我希望應用程序將ViewController推到屏幕上。這個流程是當應用程序啓動或從後臺打開時,它會檢查存儲的NSDate並將其與當前日期進行比較。如果符合條件,則顯示UIAlertView。如果選擇了名爲「Push」的按鈕,它將運行代碼以推送新視圖。這就是我需要它從AppDelegate運行的原因,因爲不能保證如果應用程序在後臺使用時可能打開哪個標籤。由於每個標籤包含一個NavigationController,我想我可以從AppDelegate中運行以下命令:從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警告消息。有什麼我可以做的建議?

+0

你可以發佈完整的警告和一些你的代碼嗎? – rishi

+0

@ rishi看到編輯的問題 – user717452

+0

我認爲你必須這樣做 - [self.tabBarController.selectedViewController.navigationController pushViewController:self.newView animated:YES]; – rishi

回答

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

評論,如果這是你期待的一個!

+0

我沒有爲我的應用程序使用Storyboard,只是XIB – user717452

+0

ViewControllerOne * tab1 = [[ViewController alloc] initWithNibName:@「ViewController」bundle:nil]];編輯此ViewControllerTwo也,並刪除UIStoryBoard – Sandeep

+0

我想要標籤欄的點擊事件,就好像不打開視圖控制器,如果你沒有訪問IOS中的相機。 – ChaubeyJi

相關問題