SWIFT uiviewcontroller init
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.
Related videos on Youtube
Marianna
Updated on June 14, 2022Comments
-
Marianna almost 2 years
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 insideinitWithNibName
.Now with storyboard I'm lost. I created a
UIviewController
subclass, I tried addinginit
(nib name) andinit
(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.-
jamil ahmed over 9 yearsWhere does it crash? Is there an exception message? What have you tried to debug so far?
-
-
Marianna over 9 yearsit 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)
-
Bob Vork over 9 yearsWell 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