简体   繁体   English

如何以编程方式在 AppDelegate 中设置初始视图控制器?

[英]How to set the Initial viewController in AppDelegate Programatically?

I know that this question was asked a couple of times but I tried all the solutions and nothing really worked for me.我知道这个问题被问了几次,但我尝试了所有的解决方案,但没有一个对我有用。

I am currently working on an iOS Project and encountered a problem.我目前正在处理一个 iOS 项目并遇到了一个问题。

I am building an App which needs some personal information at the first launch.我正在构建一个在第一次启动时需要一些个人信息的应用程序。 Therefore I use a NavigationController which I set to be the Initial View Controller in Storyboard.因此,我使用了一个NavigationController ,我将其设置为 Storyboard 中的Initial View Controller

Now I would like to check in my AppDelegate whether the User already entered the Personal Data.现在我想检查我的 AppDelegate 用户是否已经输入了个人数据。 In this case I created a User Defaults KV Pair called "onboarding" of type Bool在这种情况下,我创建了一个User Defaults KV Pair called "onboarding" of type Bool

If the Value is true , I would like to change the Initial View Controller to the main TabBarController , the rest of the App uses.如果 Value 为true ,我想将初始视图控制器更改为主TabBarController ,应用程序的其余部分使用。

I hope this image can illustrate what I meant above:我希望这张图片可以说明我上面的意思: 在此处输入图片说明

I hope you can think of a way to solve this problem.我希望你能想出解决这个问题的方法。

Thanks a lot for your help in advance.非常感谢您的帮助。 Have a great day!祝你有美好的一天!

1) Open your AppDelegate.swift and add property 1) 打开你的AppDelegate.swift并添加属性

var window: UIWindow?

2) In 2) 在

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

a) initialize this window: a) 初始化这个窗口:

window = UIWindow(frame: UIScreen.main.bounds)

b) set UIViewController you needed to window 's rootViewController property: b) 将您需要的 UIViewController 设置为windowrootViewController属性:

window?.rootViewController = myViewController

c) make this window key and visible: c) 使此窗口键可见:

window?.makeKeyAndVisible()

That is all.就这些。 And don't forget to remove your storyboard from target settings: Remove storyboard from target并且不要忘记从目标设置中删除您的故事板:从目标中删除故事板

Hope its help.希望它的帮助。

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

相关问题 在 appdelegate 中设置初始视图控制器 - swift - set initial viewcontroller in appdelegate - swift 在AppDelegate Swift中更改初始ViewController - Change initial viewcontroller in AppDelegate swift 如何通过AppDelegate覆盖首次运行时的初始ViewController - How to override initial ViewController on First Run thru AppDelegate 如何在Appdelegate中关闭ViewController? - How to dismiss viewcontroller in appdelegate? 在AppDelegate中为初始ViewController使用强委托 - Using strong delegate for initial ViewController in AppDelegate 更改AppDelegate中设置的ViewController的框架 - Change the frame of a ViewController set in AppDelegate 如何使用情节提要设置条件初始ViewController - How to set conditional initial viewcontroller with storyboard 如何在故事板中设置初始ViewController的委托 - How to set the delegate of initial ViewController in a storyboard 故事板:如何使用故事板为 UIPageViewController 设置初始视图控制器? - Storyboard: how to set initial viewcontroller for UIPageViewController with storyboard? 带有 4 个视图控制器的页面 ViewController - 如何将视图 2 设置为初始视图控制器? - Page ViewController with 4 viewControllers - How to set view 2 as initial viewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM