iOS-不同情节提要视图控制器之间的转换

iOS-不同情节提要视图控制器之间的转换,ios,objective-c,storyboard,Ios,Objective C,Storyboard,我将我的项目分为两个故事板: Main.storyboard-用于经过身份验证的用户。这也是默认的故事板 Login.storyboard-用于未经身份验证的用户 应用程序代理文件: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if(user is authenticated) { [

我将我的项目分为两个故事板:

  • Main.storyboard
    -用于经过身份验证的用户。这也是默认的故事板

  • Login.storyboard
    -用于未经身份验证的用户

应用程序代理文件:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if(user is authenticated)
    {
        [self presentLoginScreen:YES];
        return YES;
    }
-(void)presentLoginScreen:(BOOL)animated{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = vc;
}
-(void)logOut{
   //clear data

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window.rootViewController = vc;
    //Show login Screen
    [self presentLoginScreen:NO];
}
应用程序代理文件中的登录屏幕:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if(user is authenticated)
    {
        [self presentLoginScreen:YES];
        return YES;
    }
-(void)presentLoginScreen:(BOOL)animated{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = vc;
}
-(void)logOut{
   //clear data

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window.rootViewController = vc;
    //Show login Screen
    [self presentLoginScreen:NO];
}
注销时,将显示登录屏幕。我定义了此应用程序内委托文件:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    if(user is authenticated)
    {
        [self presentLoginScreen:YES];
        return YES;
    }
-(void)presentLoginScreen:(BOOL)animated{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = vc;
}
-(void)logOut{
   //clear data

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window.rootViewController = vc;
    //Show login Screen
    [self presentLoginScreen:NO];
}
从MainstryBoard的viewcontroller(比如MainstryBoardVC.m)中,注销被称为:

-(void)didTouchLogOut{
    NSLog(@"GoodBye");
    AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
    [appDelegateTemp logOut];
}
这引起了很多问题:

  • 首先,当我注销并转到登录屏幕时,我仍然可以在后台看到
    mainstoryboardVc.m
    ,这看起来非常笨拙

  • 我注销、重新登录并再次尝试注销时,会看到以下消息:

不建议在分离的视图控制器上显示视图控制器

并且不显示登录屏幕


我在web上尝试了许多关于两个故事板之间的视图控制器演示的答案,但没有保留历史记录,似乎什么都不起作用…

要替换UIViewController,在转换根视图控制器时需要调用一些其他方法:

目标c:

- (void)setRootViewController:(UIViewController *) newRootViewController {

    UIViewController* currentViewController = self.window.rootViewController;
    if (newRootViewController != currentViewController) {
        [currentViewController willMoveToParentViewController:nil];
        [currentViewController.view removeFromSuperview];
        [currentViewController removeFromParentViewController];
        self.window.rootViewController = newRootViewController;
    }
}
斯威夫特:

func setRootViewController(newRootViewController: UIViewController) {
    if let currentViewController = self.window?.rootViewController {
        if currentViewController != newRootViewController {
            currentViewController.willMoveToParentViewController(nil)
            currentViewController.view.removeFromSuperview()
            currentViewController.removeFromParentViewController()
            self.window?.rootViewController = currentViewController
        }
    }
}

两个
UIViewController
对象是重复的,因为您为这两个对象设置了
self.window.rootViewController
。试试看

登录

-(void)presentLoginScreen:(BOOL)animated{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
    UIViewController *vc =[storyboard instantiateInitialViewController];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
}
注销

-(void)logOut{
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:[NSBundle mainBundle]];
   UIViewController *vc =[storyboard instantiateInitialViewController];
   [self presentViewController:vc animated:YES completion:nil];
}

你把事情弄得复杂多了,却没有任何收获?为什么要使用两个不同的故事板?使用两个故事板有什么意义?因为场景数量很大,两个组件非常独立,每个故事板都是由不同的开发人员开发的。合并两个将是最后一个选择,但如果我不必接受最后一个选择,那就太好了。。