I have 5 instances of CalculatorViewController in my storyboard, but only one with identifier "secondOperationViewController". 3 of them connected with my initial ViewController named ContainerViewController using embed segues.
I want ViewController with identifier "secondOperationViewController" to call method from my ContainerViewController, but I cant correctly instantiate it, so delegate method not being called. Here is part of my code:
override func viewDidLoad() {
super.viewDidLoad()
secondOperationViewController = storyboard.instantiateViewControllerWithIdentifier("secondOperationViewController") as? CalculatorViewController
secondOperationViewController!.delegate = self
println(secondOperationViewController)
}
Any suggestions what's wrong here?
Also I found similar question:
I rewrited instantion of my CalculatorViewController to:
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
secondOperationViewController = segue!.destinationViewController as? CalculatorViewController
secondOperationViewController!.delegate = self;
}
It works, but my delegate method is being called from all 3 ViewControllers, connected to initial ViewController, so I need another solution.
So how can I call delegate method exactly from ViewController with specified identifier? Any ideas?
So, embed segue doesn't work with "instantiateViewControllerWithIdentifier" (but works with modal presented segue). I dont know why. Here is solution for embed segue:
override func viewDidLoad() {
super.viewDidLoad()
let secondOperationViewController:CalculatorViewController = childViewControllers[0] as CalculatorViewController
secondOperationViewController.delegate = self
}
In the storyboard give your segue a unique identifier:
Then in your prepareForSegue
check against that before you set the delegate, this way you know exactly when you will be setting up this delegate.
override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
// Checking to make sure this is the correct segue. This is also protection for the "as?"
if segue?.identifier == "SecondOVCSeuge" {
secondOperationViewController = segue!.destinationViewController as? CalculatorViewController
secondOperationViewController!.delegate = self;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.