3

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.

8
  • 4
    Where does it crash? Is there an exception message? What have you tried to debug so far? Commented Nov 2, 2014 at 18:47
  • How are you trying to instantiate this view controller? In a segue? in Code? Commented Nov 2, 2014 at 18:49
  • (lldb) i'm not good with debuggers but i think it crashes on the init of the nib. i started Xcode 6.1 and opened a new project TabBarController, in storyboard i have my 2 view controllers... and that's the code of the second one... that's all i've done Commented Nov 2, 2014 at 18:53
  • 1. Your init methods are not necessary here unless you're doing anything material there, which you aren't (and the setting of the title could be moved to viewDidLoad). 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! Commented Nov 2, 2014 at 18:54
  • this is a screen of my error i61.tinypic.com/2vmzdco.png Commented Nov 2, 2014 at 19:13

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it worked!!!! can you explain me better why please? i tried to put also super.init(coder:aDecoder) inside the init(coder) and it never called the nib init... i'm lost cause Xcode always want me to use init(coder)
Well in general, when you're overriding a function, the call to super should use the same signature (if you don't the nib name and bundle name will be lost). which one of the initializers is called depends on how you instantiate your class or nib

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.