IOS:模态显示登录屏幕的代码
IOS: Code to present login screen modally
我想用登录屏幕密码保护应用程序(并加入首次使用屏幕)。 SO 上的一些答案建议测试用户是否在初始屏幕的 viewdidappear 中登录,如果未登录,则以模态方式显示登录屏幕。
我试过了,但是代码不工作。有谁知道用于呈现模态视图控制器的最新代码?请注意,我在故事板中创建了登录屏幕并为其指定了故事板 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
}
}
我想用登录屏幕密码保护应用程序(并加入首次使用屏幕)。 SO 上的一些答案建议测试用户是否在初始屏幕的 viewdidappear 中登录,如果未登录,则以模态方式显示登录屏幕。
我试过了,但是代码不工作。有谁知道用于呈现模态视图控制器的最新代码?请注意,我在故事板中创建了登录屏幕并为其指定了故事板 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
}
}