Reshan Kumarasingam
Reshan Kumarasingam

Reputation: 453

Swift Delegate method never getting called

I have declared my protocol like below in the modal

protocol MapViewControllerDelegate : class {
    //func mapViewLocationSelected(sender:AnyObject, location:CLLocation, address:String)
    func didSelectMap(sender:AnyObject)
}

Also I've declared the delegate as below

weak var delegate : MapViewControllerDelegate?

Then I dismiss the screen and invoke delegate method as below

self.dismissViewControllerAnimated(true, completion: nil)
delegate?.didSelectMap(self)

In the viewcontroller which invoked the model I have following things

Declared the Modal VC as below

var mvController : MapViewController = MapViewController()

Then I set the delegate as below under viewDidLoad

mvController.delegate = self

Then I implemented the delegate methods

func didSelectMap(sender: AnyObject) {
    println("Came here")
}

But I never came to ddidSelectMap method. I've done this before and it worked, I couldn't figure out what went wrong. Any help would be appreciated.

parent viewcontroller pastebin.com/7z5vg6yJ

model viewcontroller pastebin.com/DwAa6erQ

Could it be a problem caused due to MapKit I'm using?

Thanks in advance.

Upvotes: 2

Views: 1076

Answers (2)

Jaya Mayu
Jaya Mayu

Reputation: 17257

My suggestion would be to assign the delegate in the prepareForSegue method. Also make sure to use the segue.destinationViewController to get the appropriate VC.

Something like below would do I assume.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showMapviewSegue"{
            mapVController = segue.destinationViewController as! MapViewController
            mapVController.delegate = self
        }
}

Try this, it should work.

Upvotes: 3

iRiziya
iRiziya

Reputation: 3245

First call delegate and then dismiss the controller. That may solve your problem

 delegate?.didSelectMap(self)
 self.dismissViewControllerAnimated(true, completion: nil)

Upvotes: 0

Related Questions