简体   繁体   English

Swift Delegate方法永远不会被调用

[英]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 声明模态VC如下

var mvController : MapViewController = MapViewController()

Then I set the delegate as below under viewDidLoad 然后我在viewDidLoad下将委托设置如下

mvController.delegate = self

Then I implemented the delegate methods 然后我实现了委托方法

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

But I never came to ddidSelectMap method. 但是我从来没有来过ddidSelectMap方法。 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 父级Viewcontroller pastebin.com/7z5vg6yJ

model viewcontroller pastebin.com/DwAa6erQ 模型视图控制器pastebin.com/DwAa6erQ

Could it be a problem caused due to MapKit I'm using? 可能是由于我使用的MapKit引起的问题吗?

Thanks in advance. 提前致谢。

My suggestion would be to assign the delegate in the prepareForSegue method. 我的建议是在prepareForSegue方法中分配委托。 Also make sure to use the segue.destinationViewController to get the appropriate VC. 另外,请确保使用segue.destinationViewController来获取适当的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. 试试这个,它应该起作用。

First call delegate and then dismiss the controller. 首先呼叫代表,然后关闭控制器。 That may solve your problem 那可以解决你的问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM