简体   繁体   中英

SWIFT uiviewcontroller init

I am starting to use Swift .

with Objective-C when I could avoid using storyboard, initializing a view controller was easy. I would just create a UIViewController subclass, and I would put all the initialization code inside initWithNibName .

Now with storyboard I'm lost. I created a UIviewController subclass, I tried adding init (nib name) and init (coder) but it crashes.

This is my code:

   //
    //  SecondViewController.swift
    //  tabTestWithSwift
    //
    //  Created by Marianna Ruggieri  on 02/11/14.
    //  Copyright (c) 2014 Marianna Ruggieri. All rights reserved.
    //

    import UIKit

class FirstViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        println("init with nib")
        super.init()
        tabBarItem.title = "test"
    }

    required init(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        print("init coder")
        super.init()
    }

}

See this screenshot: http://oi62.tinypic.com/10ogwsh.jpg

EDIT: I didn't do anything special. Started a TabBar project and edited the view controller as you see in the code above. That's it!!!!

Which is the correct way to initialize a UIViewController if I'm using storyboard? Where should I put all the settings for my view controller? The load is too late for certain settings.

You are creating a loop by calling super.init() where you should be calling init(nibName:nibBundleOrNil) . The super implementation of init() calls your method, which in turn calls super init again.

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