UIAlertview委托方法未在子视图控制器中调用 [英] UIAlertview Delegate Method not calling in Child View Controller
问题描述
我有两个控制器
VC A
-> Parent
和VC B
-> Child
警报视图委托方法,即
func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){}
在VC A
中声明.
当我单击警报按钮时,未调用VC B
委托方法显示警报时.
When i display alert from VC B
delegate method is not called on alert button clicked.
推荐答案
请按照下列步骤操作:
-
在VC-B中创建一个协议:
protocol AlertViewProtocol:class {
func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
}
在VC-B类中添加 var 为:
var delegate: AlertViewProtocol?
在VC-B的任何类方法中使用委托将数据发送到接收方法(即VC-A的任何方法),该方法是采用协议的任何方法.请遵循以下模式使用委托:
Use the delegate in any class method of VC-B to send the data to the receiving method (i.e. any method of VC-A), which is any method that adopts the protocol. Follow this pattern to use the delegate:
delegate?.alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
// Above statement is like a method calling (on delegate var), use your parameters accordingly
在接收类(此处为VC-A)中采用协议:
Adopt the protocol in your recieving class (here, VC-A):
class ViewControllerA: UIViewController, AlertViewProtocol {
...
...
}
在VC-A中实现委托方法:
Implement the delegate method in VC-A:
func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int) {
// Do your stuff
// when you click on the designated button in your VC-B, this method will be invoked
}
在要实例化VC-B对象的VC-A中设置委托.就我而言,这就像:
Set the delegate in VC-A where you are instantiating the VC-B object. In my case this is like:
在这里,
vcb?.delegate = self
非常重要.如果忘记使用self
设置对象的委托属性,则委托过程将无法进行.
Here,
vcb?.delegate = self
is very important. If you forget to set the delegate property of the object withself
the delegation process won't work.
@IBAction func showVCB(sender: AnyObject) {
let vcb: ViewControllerB? = self.storyboard?.instantiateViewControllerWithIdentifier("viewcontrollerB") as? ViewControllerB
vcb?.delegate = self
self.presentViewController(vcb!, animated: true, completion: nil)
}
希望,这可以帮助您了解代表如何工作的过程.
Hope, this helps you for understanding the process of how the delegates work.
这篇关于UIAlertview委托方法未在子视图控制器中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!