The Nomad
The Nomad

Reputation: 7425

Instantiate View Controller with Identifier with custom init

How would I go about instantiating a custom ViewController class with custom init by using the storyboard method?

How would calling the ProfileViewController(userId: "abc") init?

Instantiate View Controller

let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ProfileViewController") as UIViewController

Custom View Controller class

class ProfileViewController: UIViewController {

var userId = ""

init(userId: String) {
    self.userId = userId
    super.init(nibName: nil, bundle: nil)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func viewDidLoad() {
    super.viewDidLoad()

    let menuItem = UIBarButtonItem(image: UIImage(named: "MenuIcon"), style: .Plain, target: self, action: "menuBarButtonItemClicked")
    menuItem.tintColor = UIColor.whiteColor()
    self.navigationItem.leftBarButtonItem = menuItem
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Upvotes: 2

Views: 3385

Answers (1)

matteok
matteok

Reputation: 2199

I think what you are trying to do is not possible. However you can do something like this

let destViewController = mainStoryboard.instantiateViewControllerWithIdentifier("ProfileViewController") as ProfileViewController
destViewController.userId = "abc"

Upvotes: 3

Related Questions