Friday, October 17, 2014

How to initialise View controller in Swift?


Initialising ViewController in Swift is different from the Objective C. As in Swift, we have to implement the Required Designated initialiser method and they will not return any value.

If you create a subclass of UIViewController using Swift Language then your file will look like this-

import UIKit

class DetailViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

You have to implement init(coder aDecoder: NSCoder!) and init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) to initialize the ViewController manually. 

After adding the initializer method your ViewController class look like this-

class DetailViewController: UIViewController {

required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// Here you can init your properties
}

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

If you will not add this methods in your ViewController File then app will crash and it will show error “fatal error: use of unimplemented initializer” to you.

This error is occurring because we have different initialising behaviour in Swift in comparison to Objective C. Objective C automatically calls the initialising method but in swift we have to add the required initialising method manually.  

Also, Swift initialiser do not return any value as return by Objective C initialiser.

Note-
1) If you are using StoryBoard then add init(coder aDecoder: NSCoder!) in your ViewController class.
2) If you are initialising the ViewController programatically then add init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) in your ViewController class.


4 comments:

  1. if you don't mind do you have a project written in swift ?.. if could you please share would be great if you could :)

    ReplyDelete
  2. Please check my other post in which i have attached some sample project written in Swift language.

    http://creativeiphonecoding.blogspot.in/2014/10/create-custom-view-using-ibdesignable.html

    ReplyDelete
  3. var mv = MyViewController(nibName:"MyViewController", bundle:nil);
    self.view.addSubview(mv.view);

    I have a button action inside MyViewController. When I tap the button app crashes. Why?

    ReplyDelete