简体   繁体   English

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

[英]IOS: Code to present login screen modally

I want to password protect app with login screen (and join screen for first use). 我想用登录屏幕(和首次使用的加入屏幕)对应用程序进行密码保护。 Some answers on SO suggest testing if user is logged in in viewdidappear of initial screen and, if not logged in, presenting the login screen modally. SO上的一些答案建议测试用户是否以初始屏幕的外观出现在viewdid中,如果没有登录则以模态方式显示登录屏幕。

I tried this but code is not working. 我试过了,但是代码不起作用。 Does anyone know up to date code for presenting a modal view controller? 有人知道用于呈现模态视图控制器的最新代码吗? Note I created login screen in storyboard and have given it storyboard id "login". 注意我在情节提要中创建了登录屏幕,并为其指定了情节提要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
*/

Credit to @rdelmar comment, You have to presentViewController:animated:completion: (presentModalViewController:animated: that ivc. 感谢@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.

相关问题 迅速的ios tabBar didSelectItem当前登录屏幕 - swift ios tabBar didSelectItem present login screen 应用程序试图以模态方式呈现一个主动控制器 ios - Application tried to present modally an active controller ios iOS应用程序尝试以模态方式呈现活动控制器 - iOS Application tried to present modally an active controller 当前代表在iOS 5中的视图 - Present view from delegate modally in iOS 5 通过代码模态地使用segue - use segue with kind present modally by code 基于故事板的模式登录屏幕示例 - Example for login screen modally based on storyboard 如何以模态方式呈现的UIViewController的UIView总是出现在屏幕的底部? - How to have a UIView of a UIViewController that will be presented modally always be present at the bottom of the screen? 在不覆盖全屏的UIViewController中以模态形式显示UIViewController - Present UIViewController modally within a UIViewController that does not cover full screen iOS SDK:推送问题创建,而不是“模态呈现” - iOS SDK : Push Issue Creation rather than present 'modally' 应用程序尝试以模态形式显示一个主动控制器iOS 6.0.2 - Application tried to present modally an active controller iOS 6.0.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM