Xcode:在applicationDidBecomeActive中显示登录视图 [英] Xcode: Display Login View in applicationDidBecomeActive

查看:106
本文介绍了Xcode:在applicationDidBecomeActive中显示登录视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想显示一个登录屏幕 - 当应用程序启动时以及应用程序变为活动状态时将显示该屏幕。作为参考,我使用的是故事板,ARC,它是一个标签栏应用程序。

In my app I would like to show a login screen - which will be displayed when the app starts and when the app becomes active. For reference, I am using storyboards, ARC and it is a tabbed bar application.

因此我需要在 applicationDidBecomeActive中执行此过程方法:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if ( ... ) { // if the user needs to login
        PasswordViewController *passwordView = [[PasswordViewController alloc] init];
        UIViewController *myView = self.window.rootViewController;
        [myView presentModalViewController:passwordView animated:NO];
    }
}

在某种程度上这确实有效 - 我可以打电话给 viewDidAppear 中的方法,它显示了允许用户登录的警报视图。但是,这是不合需要的,我希望有一个登录文本框和其他ui元素。如果我没有调用我的登录方法,即使我在视图上放置了标签和其他元素,也没有任何反应并且屏幕保持黑色。

To an extent this does work - I can call a method in viewDidAppear which shows an alert view to allow the user to log in. However, this is undesirable and I would like to have a login text box and other ui elements. If I do not call my login method, nothing happens and the screen stays black, even though I have put a label and other elements on the view.

有没有人知道解决这个问题的方法?我的密码视图嵌入在导航控制器中,但与主故事板分离。

Does anyone know a way to resolve this? My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.

推荐答案

各种答案最终导致我一个似乎并不太复杂的答案所以我会在这里发布 - 如果我是诚实的,它实际上看起来真的很好。

A variety of answers finally led me to an answer which doesn't seem too complicated so I will post it here - and it actually looks really good if I am honest.

首先,我的密码视图是嵌入式的在导航控制器(编辑器 - >嵌入)中,它使用带有id的模态segue连接到主标签栏控制器,在我的情况下为loginModal。

Firstly, my password view is embedded in a Navigation Controller (Editor -> Embed In) and this is connected to the main tab bar controller using a modal segue with an id, in my case 'loginModal'.

applicationDidBecomeActive 方法中输入如下内容:

In the applicationDidBecomeActive method put something like this:

[self performSelector:@selector(requestPasscode) withObject:nil afterDelay:0.2f];

然后将此功能放在App Delegate中的某个位置

And then put this function somewhere in the App Delegate

-(void)requestPasscode{
    if ( /* If the user needs to login */ ) {
        [self.window.rootViewController performSegueWithIdentifier:@"loginModal" sender:self];
    }
}

这将在应用程序开始时显示您的登录视图或进入前台(例如,切换应用时)。

This will present your login view whenever the app begins or enters the foreground (for example, when switching apps).

注意:如果应用的根目录已嵌入,则上述行无效在导航控制器中。

NOTE: The above line will not work if the root of your app is embedded in a navigation controller.

但是有两个错误;


  1. 如果用户以前在取消应用时查看模态视图

  2. 如果用户在密码视图中解除了应用。

这两个都导致应用程序崩溃,因此以下行进入 applicationWillResignActive 方法。

Both of these cause the app to crash so the following line goes in the applicationWillResignActive method.

[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

它基本上驳回了所有呈现的模态视图。这可能不太理想,但模态视图通常不会用于数据输入,因此在许多情况下,这是一个理想的效果。

It basically dismisses all modal views that are presented. This may not be ideal, but modal views are more often then not, used for data entry and so in many cases, this is a desired effect.

这篇关于Xcode:在applicationDidBecomeActive中显示登录视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆