[英]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 设置为
window
的rootViewController属性:
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.