Swift - Passing value back to ViewController using protocol and delegate

99封情书 提交于 2019-12-12 04:10:20

问题


I'm new to swift and my problem right now is I want to pass the value from RoutePreviewController page back to display on ViewController page after clicking on the Navigate Button. As below picture, you can see that the RoutePreviewController is not directly segue from the ViewController.

This is my story board of the swift project on xcode

However, I tried using protocol and delegate in the code.

Here are codes that I have add to the ViewController (MainPage)

protocol HandleSelectedPath{
    func selectedPathDraw(selectedRoute:[(line:Int?,node:Int,time:Double)])
}

in viewDidLoad of ViewController

    let routePreviewPage = storyboard!.instantiateViewControllerWithIdentifier("RoutePreviewController") as! RoutePreviewController
    routePreviewPage.handleSelectedPathDelegate = self

and an extension outside the ViewController class

extension ViewController : HandleSelectedPath{
    func selectedPathDraw(selectedRoute:[(line:Int?,node:Int,time:Double)]){
        print("7687980")
        selectedPath = selectedRoute
        routeDraw()
    }
}

And in RoutePreviewController, I have delegation of the protocol.

var handleSelectedPathDelegate: HandleSelectedPath? = nil 

and the Navigate button action

@IBAction func navigateButtonAction(sender: AnyObject) {
    handleSelectedPathDelegate?.selectedPathDraw(previewPath)
    dismissViewControllerAnimated(true, completion: nil)
    definesPresentationContext = true
}

As a result, after clicking the Navigate button, it does send me back to the ViewController page but the selectedPathDraw function of the protocol is not performed. I also tried printing some random string but nothing came up as an output.


回答1:


The reference for your RoutePreviewController according to your code above only exist inside your viewDidLoad, you have to set as property instead like this:

var routePreviewPage: RoutePreviewController!

Always it's a good practice implement your delegate as a weak reference to avoid retain-cycles, so the correct way of implement your delegate and protocol should be as the following code:

protocol HandleSelectedPath: class {
   func selectedPathDraw(selectedRoute:[(line:Int?,node:Int,time:Double)])
}

RoutePreviewController:

weak var handleSelectedPathDelegate: HandleSelectedPath?

@IBAction func navigateButtonAction(sender: AnyObject) {
   handleSelectedPathDelegate?.selectedPathDraw(previewPath)
   dismissViewControllerAnimated(true, completion: nil)
   definesPresentationContext = true
}

viewDidLoad of ViewController:

routePreviewPage = storyboard!.instantiateViewControllerWithIdentifier("RoutePreviewController") as! RoutePreviewController
routePreviewPage.handleSelectedPathDelegate = self

I hope this help you.



来源:https://stackoverflow.com/questions/37244146/swift-passing-value-back-to-viewcontroller-using-protocol-and-delegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!