current position:Home>IOS startup project (I) setting rootviewcontroller

IOS startup project (I) setting rootviewcontroller

2022-01-27 03:30:49 I'm crazy about Pisces

Abstract

Just created a new project , stay AppDelegate Set in rootViewController To determine the home page of the application is the most basic processing , Because it is an infrequent process , So it's easy to ignore one of these steps , As a result, it cannot be set successfully . So write it down , For quick discovery .

Just created a iOS project , The application will be set first rootViewController, That is, the home page of the application . The general operation code is as follows :

// UIViewController  For home controller 
let homeNav = UINavigationController.init(rootViewController: UIViewController.init())
window?.rootViewController = homeNav
window?.makeKeyAndVisible()

These three lines of code need to be written in AppDelegate Of application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool Function .

When the above steps are completed , You'll find there's no window object , Then we need to AppDelegate Set in window Attribute variables :

class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    ......
}

Come here , There is no error in the project , Just run the application , Long compilation 、 After the process of initializing and starting the application , It is found that the home page is not the controller set by yourself .

Next, check the problem , The top three lines of code are also executed , Just interrupt and look window object ,window Neither nil. Looking for a long time , It is found that further processing is needed .

It's in the project , choice Info Options , notice Custom iOS Target Properties In the list , Delete Application Scene Manifest Options .

window-image

Now , Run the project again , You can successfully jump to your own home page .

that ,Application Scene Manifest What is it? , Why does it affect the setting of the home page ?

Find development documentation , notice UIApplicationSceneManifest Explanation of the options , The answer was found in the detailed explanation : If in plist Set this option in the file , Then the application supports scenes, And not in AppDelegate Handle page switching in .

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 a new project is created , It will be found that in addition to AppDelegate.swift Outside the document , One more SceneDelegate.swift file . And this file can only be in iOS 13 And above . At present, it is not very sure about the connection and difference between the two documents , What kind of scene to use SceneDelegate object , Learn more later , Use it again .

Digression

Hasty time , What you say may not be comprehensive , What problems did you encounter in the process of viewing , Leave me a message in the comment area , I will reply as soon as possible .

copyright notice
author[I'm crazy about Pisces],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270330453641.html

Random recommended