我在我的应用中实现了UINavigationController
,我想要实现的是以编程方式确定应用是否已经启动。我需要这个来确定我应该向用户显示哪个视图。如果这是第一次运行,我需要在任何其他时间显示不同的视图。
当我不使用UINavigationController
时很容易,但在这种情况下,当我使用我的方法时,我正在摆脱UINavigationController
层次结构。
这是我用来确定首次运行的方法:
+ (void)executeBlockAtTheFirstRun:(void (^)())firstRunBlock atAnotherRun:(void (^)())anotherRunBlock
{
// Checking whether HasAlreadyBeenLaunched key is set to be YES
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasAlreadyBeenLaunched"]) {
// Running block given by the user when this isn't the first run of the app
anotherRunBlock();
// Uncomment this if you want the log
//NSLog(@"Application has already been launched");
} else {
// Seeting the bool value for key HasAlreadyBeenLaunched and synchronising user defaults
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasAlreadyBeenLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
// Running the block with code provided by the user for the first run of the app
firstRunBlock();
// Uncommeent this f you want the log
// NSLog(@"This is the first run");
}
}
所以当我在完成块中输入类似的东西时:
NSString *storyboardID = [self hasEverBeenLaunched]? @"MainView" : @"LoginView";
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardID];
将所有内容放入 AppDelegate.m 我能够管理视图,但正如我所说,我的UINavigationController
层次结构已经消失。我应该怎样做才能使一切运转起来呢?
答案 0 :(得分:2)
如果您想要导航控制器,请制作导航控制器!而不是
self.window.rootViewController =
[self.window.rootViewController.storyboard
instantiateViewControllerWithIdentifier:storyboardID];
说
UIViewController* root =
[self.window.rootViewController.storyboard
instantiateViewControllerWithIdentifier:storyboardID];
UINavigationController* nav =
[UINavigationController alloc] initWithRootViewController: root];
self.window.rootViewController = nav;
答案 1 :(得分:2)
在UINavigationController
中以可视方式将根视图控制器设置为storyboard
。
尝试以下方法:
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasAlreadyBeenLaunched"]) {
MainViewController *mainViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MainViewController"];
[self.navigationController setViewControllers:[NSArray arrayWithObject:mainViewController] animated:YES];
} else {
LoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"loginViewController"];
[self.navigationController setViewControllers:[NSArray arrayWithObject:loginViewController] animated:YES];
}
或者您可以直接设置您的根视图控制器,如@matt答案描述。
答案 2 :(得分:1)
您必须先创建导航控制器并设置为根视图控制器:
NSString *storyboardID = [self hasEverBeenLaunched]? @"MainView" : @"LoginView";
UIViewController *vc = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardID];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
答案 3 :(得分:0)
在MainViewController中创建故事板,名称为“ShowLoginView”的segue推送LoginViewController。代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (!self.hasEverBeenLaunched) dispatch_async(dispatch_get_main_queue(), ^(){
[[(UINavigationController*)self.window.rootViewController visibleViewController] performSegueWithIdentifier:@"ShowLoginView" sender:nil];
});
return YES;
}