在appDelegate中第一次呈现视图
目标:
当我的应用程序启动时 – 我需要它在显示视图才能进入“主页”屏幕。 它是一个标签栏应用程序,此视图不是标签栏的一部分。
我正在使用Storyboards和Xcode 5 – 仅限iOS7的应用程序。
问题:
我有代码可以检查应用程序是否首次启动。 基于此,我想向用户呈现一次性视图。
我试过的:
以下代码位于应用程序的appDelegate中,因为这是它全部启动的地方。 我在那里调用以下代码:
-(void)showCountrySettings { if (self.termsHaveBeenAccepted){ BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:@"View" bundle:nil]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"]; [self.navigationController pushViewController:vc animated:YES]; }
我得到编译错误,因为[self.navigationController..]
不存在。 也不是[self.tabbarcontroller...];
这很明显,因为我没有为这些设置属性 – 但是如何解决此问题并将标签栏连接到故事板?
我错过了什么?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. if(!isAgreementAccepted) { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDD"]; self.window.rootViewController=vc; } return YES; }
如果未接受协议,则在用户单击“接受”按钮时将T&C viewController
设置为TabBarviewController
,然后将TabBarviewController
设置为root。
你可以通过应用委托任何地方访问寡妇对象
[[[UIApplication sharedApplication]delegate] window].rootViewController=tabViewController.
更改窗口programaticaly的rootviewcontroller
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIStoryboard *aStoryBoard=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; UITabBarController *aTabCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:@"Tab"]; FirstVC *aFirstCtrl=[aStoryBoard instantiateViewControllerWithIdentifier:@"First"]; if(self.termsHaveBeenAccepted) self.window.rootViewController=aFirstCtrl; else self.window.rootViewController=aTabCtrl; return YES; }
这肯定会对我测试过。
如果Tabbarcontroller是您的根视图控制器,那么使用下面的代码将解决您的appdelegate中的问题。
-(void)showCountrySettings { if (self.termsHaveBeenAccepted){ BBCounterySettingsViewController *countrySettings = [[BBCounterySettingsViewController alloc]initWithNibName:@"View" bundle:nil]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"]; UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; [tabBarController presentModalviewcontroller:vc animated:YES completionblock:nil]; }
添加tabbarcontroller的viewcontroller superview作为第一次调用tabbarcontroller单独