-2

I have two controllers

VC A -> Parent and VC B -> Child

Alert view delegate method i.e

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

is declared in VC A.

When i display alert from VC B delegate method is not called on alert button clicked.

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
Aabid Khan
  • 27
  • 1
  • 7
  • 1
    Add `AlertView` code to your question. – Morteza Soleimani Jan 16 '17 at 06:28
  • Add the whole code, how are you setting and calling delegate method? – iphonic Jan 16 '17 at 06:32
  • func showAlert(){ var createAccountErrorAlert: UIAlertView = UIAlertView() createAccountErrorAlert.delegate = self createAccountErrorAlert.tag = sender.tag createAccountErrorAlert.accessibilityIdentifier = "ReportAbuse" createAccountErrorAlert.title = "Confirm" createAccountErrorAlert.message = "Testing Alert" createAccountErrorAlert.addButtonWithTitle("Cancel") createAccountErrorAlert.addButtonWithTitle("Ok") createAccountErrorAlert.show() } – Aabid Khan Jan 16 '17 at 06:39
  • when i call showAlert() from child vc i.e VC B delegate method is not called – Aabid Khan Jan 16 '17 at 06:40
  • show the code of VC A , Parent VC B, and Child so that we can understand the problem better and tell you the solution.. – Anil Kumar Jan 16 '17 at 06:43

3 Answers3

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

Create an alert view delegate object in parent class VC A

weak var delegate:alertViewDelegate() ?= nil

Implement the delegate in VC B and Set the delegate object in VC B

let alertview = alertView()
alertview.delegate  = self
Lineesh K Mohan
  • 1,702
  • 14
  • 18
0

You are setting AlertView delegate to self, Self means Child, Change delegate to VC A.

alertView.delegate = self.parent?.parent
Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
0

Follow these step by step:

  1. Create a protocol in your VC-B as:

    protocol AlertViewProtocol:class {
        func alertView(View: UIAlertView!, clickedButtonAtIndex buttonIndex: Int)
    }
    
  2. Add a var in your VC-B class as:

    var delegate: AlertViewProtocol?
    
  3. 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
    
  4. Adopt the protocol in your recieving class (here, VC-A):

    class ViewControllerA: UIViewController, AlertViewProtocol {
    ...
    ...
    }
    
  5. 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
    }
    
  6. Set the delegate in VC-A where you are instantiating the VC-B object. In my case this is like:

Here, vcb?.delegate = self is very important. If you forget to set the delegate property of the object with self 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.

nayem
  • 7,285
  • 1
  • 33
  • 51