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.

Mario Boss
  • 1,784
  • 3
  • 20
  • 43
Marianna
  • 133
  • 2
  • 2
  • 5
  • 4
    Where does it crash? Is there an exception message? What have you tried to [debug](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) so far? – i_am_jorf Nov 02 '14 at 18:47
  • How are you trying to instantiate this view controller? In a segue? in Code? – Abizern Nov 02 '14 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 – Marianna Nov 02 '14 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!** – Rob Nov 02 '14 at 18:54
  • this is a screen of my error http://i61.tinypic.com/2vmzdco.png – Marianna Nov 02 '14 at 19:13
  • That's your console output, not your crash log. – Abizern Nov 02 '14 at 19:50
  • edited with the pic and the code and more explaination – Marianna Nov 02 '14 at 19:57
  • i wonder why i got -3 just for a simple question. Computer science was built with the spirit of sharing and helping people learning. Here it seems it's full of pretentious teachers that are just challenging each other to feel better with themselves. So i'm here asking for help, got just -3 point and no help with my issue. – Marianna Nov 02 '14 at 20:23

1 Answers1

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.

Bob Vork
  • 2,927
  • 28
  • 32
  • 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) – Marianna Nov 02 '14 at 20:50
  • 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 – Bob Vork Nov 02 '14 at 20:53