Pass values between the controller

39 Views Asked by At

I use Storyboard create a view controller,but with the code instantiation,then pass the value to the controller. I know it will be called "initWithCoder:", but in the method the property is nil. I get the property values form somewhere?

2

There are 2 best solutions below

2
Nicolas Miari On

If you are creating your view controller through a storyboard segue, use the source view controller's prepareForSegue() method, as explained in this answer to a similar question.

1
Timur Mustafaev On

Let's say you have viewControllers: A (root) and B (presenting). You need to use prepareForSegue method from A viewController:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let bView = segue.destinationViewController as? BViewController{
            bView.yourDataProperty = dataItem
        }
    }
}

Make sure your sender is presenting BViewController in Storyboard. Otherwise this method won't be called