简体   繁体   English

iOS 5从AppDelegate打开特定的ViewController

[英]IOS 5 Opening a specific ViewController from the AppDelegate

I am really new to IOS so I apologize if this questions is not worded clearly. 我对IOS真的很陌生,因此如果措辞不明确,我深表歉意。 I have tried searching around but I have not found exactly what I am looking for. 我已经尝试过搜索,但是没有找到我想要的东西。

basically in my AppDelegate applicationDidBecomeActive method, I am making a call to my webservice to make sure that the user is still a valid user, and to pull down some refrehsed data, or kick them back to the login page if they are no longer valid. 基本上是在我的AppDelegate applicationDidBecomeActive方法中,我正在调用我的Web服务,以确保该用户仍然是有效用户,并拉下一些刷新的数据,或者如果它们不再有效,则将其踢回到登录页面。

The part that I am having trouble with is the second part. 我遇到麻烦的部分是第二部分。 How can I load and show and specific ViewController(in this case the loginViewController) when the user is found to be invalid? 发现用户无效时,如何加载和显示特定的ViewController(在本例中为loginViewController)? I want to let the normal viewController flow happen when they are valid, which is is doing fine, but I can not figure out how to launch a specific viewController when I need to from AppDelegate. 我想让正常的viewController流在它们有效时发生,这很好,但是当我需要从AppDelegate中启动时,我不知道如何启动特定的viewController。

Any ideas? 有任何想法吗?

I think I got it! 我想我明白了! I used this code in the AppDelegate to display the ViewController I needed. 我在AppDelegate中使用了此代码来显示所需的ViewController。

UIViewController *loginController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    UINavigationController *loginNavController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"LoginNavController"];
    UIViewController *currentVC = self.window.rootViewController;
    _window.rootViewController = loginNavController;
    [currentVC presentViewController:loginNavController animated:NO completion:nil];

For simplicity, lets say you have a one view app (not nav controller, not tab bar controller - the solution scales but easier to explain). 为简单起见,假设您有一个单视图应用程序(不是导航控制器,也不是选项卡栏控制器-解决方案可以扩展,但更易于解释)。 When you get the appDelegate message that the app launched, then make a UIImageView the root view and show your launch image (user thinks you are still booting up). 当您收到应用程序启动的appDelegate消息时,请在根视图中创建UIImageView并显示您的启动图像(用户认为您仍在启动)。 Try to log in, and do this in some other object (not a view controller). 尝试登录,然后在其他对象(不是视图控制器)中执行此操作。 If you succeed, you make your desired view the rootView, and users sees it. 如果成功,则使所需的视图成为rootView,用户将看到该视图。 If the login fails, then you makea login window the rootView. 如果登录失败,则使登录窗口成为rootView。 The key here is to have an object that is driving this and can interact with the appDelegate. 这里的关键是要有一个驱动该对象并可以与appDelegate交互的对象。 You could also add this functionality to the appDelegate itself. 您也可以将此功能添加到appDelegate本身。

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

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