简体   繁体   中英

Programmatically Setting Root View Controller in Swift

I'm not sure why this is so trivial, but my Swift translation has been a slow one. Anyways, I can't figure out what it is complaining about. All I am trying to do is set the root view controller, and the compiler spits an error saying:

"Splash pageController does not have a member named init"

Here's my app delegate:

var window: UIWindow?

var CLIENT_KEY = c_key()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

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

    var url_string = "XXXX=\(CLIENT_KEY.client_key)"

    var g_home_url = String.stringWithContentsOfURL(NSURL.URLWithString(url_string), encoding: NSUTF8StringEncoding, error: nil)

    if (g_home_url? != nil){

        NSUserDefaults.standardUserDefaults().setObject(g_home_url, forKey: "showUrl")

    }

    let DocumentsDirectory = NSSearchPathDirectory.DocumentationDirectory

    let UserDomainMask = NSSearchPathDomainMask.UserDomainMask

    if let paths = NSSearchPathForDirectoriesInDomains(DocumentsDirectory, UserDomainMask, true){
        if paths.count > 0{
            if let dirPath = paths[0] as? String {

               // let url: NSURL = NSURL.URLWithString("\(g_home_url)/XXX\(CLIENT_KEY.client_key)")

              //  println(url)
                var err: NSError?

                var g_home_url = NSUserDefaults.standardUserDefaults().objectForKey("showUrl") as String

                println(g_home_url)
                var image = UIImage(data: NSData(contentsOfURL: NSURL.URLWithString("\(g_home_url)XXX=\(CLIENT_KEY.client_key)")))

                let writePath = dirPath.stringByAppendingPathComponent("splash_page.png")
                UIImagePNGRepresentation(image)
            }
        }
    }
//This is where the error shows up
    var rootview: SplashViewController = SplashViewController()
    if let window = window {

        window.rootViewController = rootview;
    }

SplashViewController

class SplashViewController: UIViewController {

}

Very basic I know. Normally, in Objective-C I would init a nib name and set the view controller to that. What's the main difference in Swift?

This line:

var rootview: SplashViewController = SplashViewController()

...calls SplashViewController's init method. But you have not given SplashViewController any init method. Give it one.

What I usually do is specify the nib in my init override; for example:

override init() {
    super.init(nibName:"SplashViewController", bundle:nil)
}

If you don't do something along those lines, the view controller won't find its nib and you'll end up with a black screen.

Note that that code will very likely net you a new error message complaining that you didn't implement init(coder:) ; so you'll have to implement it, even if only to throw an error if it is accidentally called:

required init(coder: NSCoder) {
    fatalError("NSCoding not supported")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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