iOS - 不同Storyboard视图控制器之间的转换

时间:2015-12-12 19:10:21

标签: ios objective-c storyboard

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

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

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

App委托文件:

- (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;
}

注销时,会显示登录屏幕。我在app委托文件中定义了这个:

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

从MainStoryBoard的视图控制器(letsay mainstoryboardVc.m),logOut被称为:

-(void)didTouchLogOut{
    NSLog(@"GoodBye");
    AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
    [appDelegateTemp logOut];
}

这引起了很多问题:

  • 首先,当我退出并移至登录界面时,我仍然可以在背景中看到mainstoryboardVc.m看起来非常笨拙。

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

  

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

并且未显示登录屏幕。

我在两个故事板之间的视图控制器演示文稿上尝试了许多答案而没有保留历史记录,似乎没有任何工作......

2 个答案:

答案 0 :(得分:0)

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

目标-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
        }
    }
}

答案 1 :(得分:0)

两个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];
}