UIAlertview委托方法不在子视图控制器中调用

时间:2017-01-16 06:22:51

标签: ios swift uialertview xcode6.4 uialertviewdelegate

我有两个控制器

VC A - > ParentVC B - > Child

警报视图委托方法,即

func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int){}

VC A中声明。

当我显示来自VC B的警告时,不会在单击警告按钮时调用委托方法。

3 个答案:

答案 0 :(得分:0)

$.ajax({
  type: "POST",
  url: url,
  data: <?php echo $vidarray["id"]; ?>,
  dataType: "text",
  success: function(result) {
    //result downloaded so we call parseJSON function
    //and pass downloaded result
    var json = $.parseJSON(result);
    //Not sure what to do after this
 }

在父类VC A中创建警报视图委托对象

protocol alertViewDelegate:class {
    func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
}

在VC B中实现委托并在VC B中设置委托对象

weak var delegate:alertViewDelegate() ?= nil

答案 1 :(得分:0)

您正在设置AlertView委托给自己,自我意味着Child,将委托更改为VC A.

alertView.delegate = self.parent?.parent

答案 2 :(得分:0)

按照以下步骤操作:

  1. 在VC-B中创建协议

    protocol AlertViewProtocol:class {
        func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
    }
    
  2. 在VC-B类中添加 var

    var delegate: AlertViewProtocol?
    
  3. 在VC-B的任何类方法中使用委托将数据发送到接收方法(即VC-A的任何方法),这是采用该协议的任何方法。按照此模式使用委托:

    delegate?.alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
    // Above statement is like a method calling (on delegate var), use your parameters accordingly
    
  4. 在您的接收课程(此处为VC-A)中采用该协议:

    class ViewControllerA: UIViewController, AlertViewProtocol {
    ...
    ...
    }
    
  5. 在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
    }
    
  6. 在VC-A中设置委托,您将在其中实例化VC-B对象。就我而言,这就像:

  7.   

    在这里,vcb?.delegate = self非常重要。如果您忘记使用self设置对象的委托属性,则委派过程将不起作用。

        @IBAction func showVCB(sender: AnyObject) {
            let vcb: ViewControllerB? = self.storyboard?.instantiateViewControllerWithIdentifier("viewcontrollerB") as? ViewControllerB
            vcb?.delegate = self
            self.presentViewController(vcb!, animated: true, completion: nil)
        }
    

    希望,这有助于您了解代表的工作过程。