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.
initmethods are not necessary here unless you're doing anything material there, which you aren't (and the setting of thetitlecould be moved toviewDidLoad). 2. The problem undoubtedly does not rest in the above code, but with how you're instantiating this view controller. Show us the code that creates/uses this view controller and/or tell us how you configured your storyboard. When you say "it crashes", that's not very informative: You have to tell us what the full error message was!