2

我的UINavigationController在我的应用程序中实现,我试图实现的是以编程方式确定应用程序是否已启动。我需要这个来确定我应该向用户展示哪个视图。如果这是第一次运行,那么我需要在任何其他时间显示不同的视图。在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层次结束了。我应该怎么做才能使所有工作都像它应该那样?

回答

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; 
+0

谢谢这是我一直在寻找的部分,但有了这个解决方案,所有层次结构都丢失了......我希望找到像调用'rootViewController'这样的东西,并在其中确定第一次运行,并在必要时推送到另一个视图去拥有这个后退按钮。 – cojoj

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

在故事板从MainViewController赛格瑞名称为“ShowLoginView创建“这推动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; 
} 
+0

这其实很棒!但我得到“推塞格只能在源控制器由UINavigationController实例管理时使用”......奇怪是因为所有内容都嵌入在“UINavigationController”中 – cojoj

+0

使其成为模态塞格而不是推式 –

+0

@Cojoj,我编辑过在rootViewController'UINavigatonController'的情况下回答 –

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

    } 

,或者你可以直接继续设置像@马特回答你的根的viewController描述。

相关问题