Xcode:在 applicationDidBecomeActive 中显示登录视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13516481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:10:26  来源:igfitidea点击:

Xcode: Display Login View in applicationDidBecomeActive

iphoneobjective-ciosxcode

提问by Patrick

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.

在我的应用程序中,我想显示一个登录屏幕 - 将在应用程序启动和应用程序激活时显示。作为参考,我使用的是故事板、ARC,它是一个标签栏应用程序。

I therefore need to do the process in the applicationDidBecomeActivemethod:

因此我需要在方法中做这个过程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];
    }
}

To an extent this does work - I can call a method in viewDidAppearwhich 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.

在某种程度上这确实有效 - 我可以调用一个方法,viewDidAppear其中显示一个警报视图以允许用户登录。但是,这是不可取的,我想要一个登录文本框和其他 ui 元素。如果我不调用我的登录方法,即使我在视图上放置了标签和其他元素,也不会发生任何事情并且屏幕保持黑色。

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

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

回答by Patrick

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.

各种各样的答案最终让我找到了一个看起来不太复杂的答案,所以我会在这里发布它 - 如果我诚实的话,它实际上看起来非常好。

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'.

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

In the applicationDidBecomeActivemethod put something like this:

applicationDidBecomeActive方法中放置如下内容:

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

And then put this function somewhere in the App Delegate

然后把这个函数放在 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.

注意:如果您的应用程序的根目录嵌入在导航控制器中,则上述行将不起作用。

There are however two bugs;

但是有两个错误;

  1. If the user was previously viewing a modal view when they dismissed the app
  2. If the user dismissed the app on the password view.
  1. 如果用户在关闭应用程序时之前正在查看模态视图
  2. 如果用户在密码视图中关闭了应用程序。

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

这两种情况都会导致应用程序崩溃,因此方法中包含以下行applicationWillResignActive

[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.

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

回答by Danil

You should init PasswordViewControllerviewcontroller from xib or if you store UI in Storyboard you should use Segue for present this controller.

您应该PasswordViewController从 xib初始化视图控制器,或者如果您将 UI 存储在 Storyboard 中,您应该使用 Segue 来呈现此控制器。

I can't say about another parts but that part seems to me very weird.

我不能说其他部分,但那部分在我看来很奇怪。

My passcode view is embedded in a Navigation Controller, but is detached from the main storyboard.

我的密码视图嵌入在导航控制器中,但与主故事板分离。

in storyboards you can store view controllers and view inside of view controllers so it's not good to store some view outside of viewcontroller because you will not be able to load this view from storyboard after receiving memory warning. Please correct me if I didn't get what do you mean.

在故事板中,您可以存储视图控制器和视图控制器内部的视图,因此在视图控制器之外存储一些视图是不好的,因为在收到内存警告后您将无法从故事板加载此视图。如果我没有明白你的意思,请纠正我。

If we are going by your way there is no difference load PasswordViewControllerat applicationDidBecomeActiveor at your first view controller at Storyboards because you calling present view controller from first loaded view controller. So you can do it in your first view controller. Also you can store some hidden view inside of your first viewcontroller and show this view if the user needs to login.

如果我们用自己的方式去那里是没有区别的负载PasswordViewControllerapplicationDidBecomeActive或在你的第一个视图控制器在故事板,因为你的呼唤从第一加载视图控制器当前视图控制器。所以你可以在你的第一个视图控制器中做到这一点。您还可以在第一个视图控制器中存储一些隐藏视图,并在用户需要登录时显示此视图。

I tested it. So at first your controller become loaded and then you got method applicationDidBecomeActive. So it's better to put your code inside -(void)viewDidAppear:animatedmethod of your first viewcontroller.

我测试了它。所以一开始你的控制器被加载,然后你得到了方法applicationDidBecomeActive。所以最好把你的代码放在-(void)viewDidAppear:animated你的第一个视图控制器的方法中。

Best regards, Danil

最好的问候,丹尼尔