Marianna
Marianna

Reputation: 133

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.

Upvotes: 3

Views: 10168

Answers (1)

Bob Vork
Bob Vork

Reputation: 2947

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.

Upvotes: 6

Related Questions