SWIFT: No idea how to get back the selected value from a popover to the calling controller

心不动则不痛 提交于 2019-12-10 10:38:16

问题


I just going crazy on Swift Popover “return” values. I am new to Objectiv-C as well as SWIFT but I try to focus on SWIFT.

I checked out tutorials around Google and StackOverflow about how to manage iOS popovers, learned a lot but the last peace I couldn’t make it. It is great so see how easy it is made using Swift and Xcode 6, love it, but I could not figure out how to get back the selected value from my popover to my calling view controller.

So here is my problem: (SIDENOTE: I am using SWIFT and do all using storyboard)

I have created a master ViewController with a button to select currencies. This button opens a “select currency” popover (linked to the CurrencyTableViewController (CTV) by CTRL-Dragging it to the CTV-Controller.

So far so good. The thing is, I have no idea how to get back the selected table row (currency) from the CTV-Table ;-( So I need the selected currency (table row) in the calling ViewController.

This is an excerpt from my ViewController (which is calling the popover)

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate 
[...]
// This button is calling the popover
@IBAction func buttonCurrency(sender: AnyObject) {
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let controller = segue.destinationViewController as? CurrencyTableViewController {
controller.popoverPresentationController?.delegate = self
return
}
}
[...]

Hopefully somebody can help me with that missing last mile how to get back the selected row value back to my ViewController.

Thanks in advance

Cheers

John


回答1:


I made quick example, hope it helps:

// This is you popover's class

@objc protocol CurrencySelectedDelegate {
    func currencySelected(currName: String)
}

class MyPopOverController: UIViewController {


    weak var delegate: CurrencySelectedDelegate?


    @IBAction func readyButtonPressed(sender: AnyObject) {

    // Do what you want

    delegate?.currencySelected("Euro/Dollar etc....")

    // close popover
    }
}

// ViewController
class ViewController: UIViewController, CurrencySelectedDelegate {

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "mySegue" { // your identifier here
            let controller = segue.destinationViewController as! MyPopOverController
            controller.delegate = self
        }
    }

}

And remember just declare that currencySelected function in your ViewController.



来源:https://stackoverflow.com/questions/29398990/swift-no-idea-how-to-get-back-the-selected-value-from-a-popover-to-the-calling

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