SWIFT uiviewcontroller init

2019-07-15 10:35发布

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()
    }

}

it crashes. this is the crash picture : 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 did load is too late for certain settings.

1条回答
\"骚年 ilove
2楼-- · 2019-07-15 11:11

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.

查看更多
登录 后发表回答