Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities
Abstract
Setting rootViewController in the AppDelegate to determine the front page of an application is a basic process when you create a new project. Because it is an infrequent process, it is easy to ignore one of the steps and fail to set it. So write it down for quick reference.
When you create an iOS project, you start by setting the app’s rootViewController, which is the app’s home page. The general operation code is as follows:
/ / UIViewController controller for front page let homeNav = UINavigationController. Init (rootViewController: UIViewController.init()) window? .rootViewController = homeNav window? .makeKeyAndVisible()Copy the code
The application of the three lines of code need to write the AppDelegate (_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool.
When you’re done, you’ll notice that there’s no window object, so you’ll need to set the window property variable in the AppDelegate:
class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? . }Copy the code
At this point, the project does not report any errors and runs the application normally. After a long process of compiling, initializing and launching the application, it is found that the home page is not the controller you set up.
Now, let’s check, the top three lines of code are also executed, but the break point is to look at the window object, window is not nil. Search for a long time, found that still need to do a step processing.
Go to the project, select Info, see the Custom iOS Target Properties list, and delete the Application Scene Manifest.
At this time, then run the project, you can successfully jump to their Settings of the home page.
So, what is the Application Scene Manifest and why does it affect setting the home page?
Find development documentation, see UIApplicationSceneManifest option, found the answer in the detailed explanation: if you set this option in the file, then application support scenes, and cannot be handled in the AppDelegate switch of the page.
Discussion
The presence of this key indicates that the app supports scenes and does not use an app delegate object to manager transitions to and from the foreground or background.
When you create a new project, you’ll find a scenedelegate. swift file in addition to the appdelegate. swift file. This file can only be used on iOS 13 or later. I’m not sure how the two files are related or different. What kind of scenarios should I use the SceneDelegate object in? I’ll use it later when I know more about it.
digression
Time is short and what you said may not be comprehensive. If you have any problems during your review, please leave a message in the comment section and I will reply as soon as possible