繁体   English   中英

IOS:以代码形式显示登录屏幕的代码

[英]IOS: Code to present login screen modally

我想用登录屏幕(和首次使用的加入屏幕)对应用程序进行密码保护。 SO上的一些答案建议测试用户是否以初始屏幕的外观出现在viewdid中,如果没有登录则以模态方式显示登录屏幕。

我试过了,但是代码不起作用。 有人知道用于呈现模态视图控制器的最新代码吗? 注意我在情节提要中创建了登录屏幕,并为其指定了情节提要ID“ login”。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //if not logged in, modally present login screen here.

    if(![[NSUserDefaults standardUserDefaults] boolForKey:@"loggedIn"]) {
        // go to login screen
        NSLog(@"not logged in");//this fires so logic is ok
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"login"];//this line gives warning it is not being used
    } else {
        // go to main screen
    }
} 
/*perhaps I should call this somewhere?

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
*/

感谢@rdelmar的评论,您必须具有presentViewController:animated:completion: (presentModalViewController:animated:该ivc。

- (void)viewDidAppear:(BOOL)animated
{
   [super viewDidAppear:animated];
   //if not logged in, modally present login screen here.

   if(![[NSUserDefaults standardUserDefaults] boolForKey:@"loggedIn"]) {
       // go to login screen
       NSLog(@"not logged in");//this fires so logic is ok
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
       UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"login"];//LOOK AT NEXT LINE
       [self presentViewController:ivc animated:YES completion:nil]; //THIS LINE IS MISSING.
  } else {
    // go to main screen
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM