[英]Push View Controller From AppDelegate and Tab Bar
My app is setup with a Tab Bar Controller as the RootViewController
, and each Tab has a NavigationController
in it. 我的应用程序使用Tab Bar Controller作为
RootViewController
,每个Tab都有一个NavigationController
。 When certain actions are performed, I want the app to push a ViewController
onto the screen. 当执行某些操作时,我希望应用程序将
ViewController
推送到屏幕上。 The flow of this is that when the app starts, or opens from background, it checks a stored NSDate and compares it to the current date. 这个流程是当应用程序启动或从后台打开时,它会检查存储的NSDate并将其与当前日期进行比较。 If the right condition is met, it shows a
UIAlertView
. 如果满足正确的条件,则显示
UIAlertView
。 If the button I named "Push" is selected, it runs the code to push the new view. 如果选择了名为“Push”的按钮,它将运行代码以推送新视图。 This is the reason I need it to be ran from the
AppDelegate
, as there is no guarantee what tab may be open if the app is being used in the background. 这就是我需要从
AppDelegate
运行它的原因,因为如果在后台使用该应用程序,则无法保证可以打开哪个选项卡。 Since every tab contains a NavigationController
, I thought I could run this from the 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];
}
}
}
I get a warning message that says UIViewController may not respond to -pushViewController:animated
. 我收到一条警告消息,说
UIViewController may not respond to -pushViewController:animated
。 Any suggestions as to what else I could do? 关于我还能做什么的任何建议?
The return type for selectedViewController is UIViewController, so you need to tell the compiler that it's actually a navigation controller. selectedViewController的返回类型是UIViewController,因此您需要告诉编译器它实际上是一个导航控制器。 You do that with a cast,
你用演员表演,
[(UINavigationController *)self.tabBarController.selectedViewController pushViewController:self.newView animated:YES];
Try this!! 尝试这个!!
- (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];
}
Comment here if this is the one you are looking forward!! 如果这是你期待的那个,请在这里评论!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.