[英]Delegate not being called (instantiateViewControllerWithIdentifier)
I have 5 instances of CalculatorViewController in my storyboard, but only one with identifier "secondOperationViewController". 我的故事板中有5个CalculatorViewController实例,但是只有一个标识符为“ secondOperationViewController”的实例。 3 of them connected with my initial ViewController named ContainerViewController using embed segues.
他们中的3个使用嵌入的segues连接到我最初的名为ContainerViewController的ViewController。
I want ViewController with identifier "secondOperationViewController" to call method from my ContainerViewController, but I cant correctly instantiate it, so delegate method not being called. 我希望带有标识符“ secondOperationViewController”的ViewController从我的ContainerViewController调用方法,但是我无法正确地实例化它,因此未调用委托方法。 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: 我也发现了类似的问题:
Delegate in Swift-language 代理Swift语言
I rewrited instantion of my CalculatorViewController to: 我将CalculatorViewController的实例重写为:
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. 它可以工作,但是我的委托方法是从连接到初始ViewController的所有3个ViewController调用的,因此我需要另一个解决方案。
So how can I call delegate method exactly from ViewController with specified identifier? 那么,如何从具有指定标识符的ViewController中完全调用委托方法呢? Any ideas?
有任何想法吗?
So, embed segue doesn't work with "instantiateViewControllerWithIdentifier" (but works with modal presented segue). 因此,embed segue不适用于“ instantiateViewControllerWithIdentifier”(但适用于模式呈现的segue)。 I dont know why.
我不知道为什么。 Here is solution for embed segue:
这是嵌入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: 在情节提要中为您的segue赋予唯一标识符:
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. 然后在设置
prepareForSegue
之前,在prepareForSegue
检查,以这种方式确切地知道何时设置该代理。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.