Xcode Swift将字符串从弹出视图传递到主VC

时间:2016-11-12 11:53:23

标签: ios swift xcode segue popover

我试图将popOverViewController中的字符串中的数据作为字符串传递回mainVC。但我无法弄清楚如何做到这一点,所以请帮助任何人。

我的mainVC segue func:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "popoverSegue" {
        let popoverViewController = segue.destinationViewController 
        popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
        popoverViewController.popoverPresentationController!.delegate = self

    }
}

我的popOverVC:

我试图将valueSelected传递回mainVC

var pickerString = NSArray() as AnyObject as! [String]
var valueSelected = String()

@IBOutlet weak var picker: UIPickerView!

@IBAction func pickButton(sender: AnyObject?) {

    // need to send valueSelected back to previous navigated view
    print("Value: ", valueSelected)
    dismissViewControllerAnimated(true, completion: nil)
}
  

需要将valueSelected字符串发送回上一个导航视图。

2 个答案:

答案 0 :(得分:1)

为此,您可以在swift中使用 closures 。以下是如何执行此操作:

<强> FirstViewController.swift

class FirstViewController: UIViewController
{
    override func viewDidLoad()
    {
    }

    @IBAction func onButtonTap(_ sender: UIButton)
    {
        self.performSegue(withIdentifier: "popoverSegue", sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "popoverSegue"
        {
            let popoverViewController = segue.destination as!PopOverViewController
            popoverViewController.modalPresentationStyle = UIModalPresentationStyle.popover
            popoverViewController.popoverPresentationController!.delegate = self
            popoverViewController.completionHandler = {(valueSelected : String?) in
                if let valueSelected = valueSelected
                {
                    print(valueSelected)
                    //Write yout code here
                }
            }
        }
    }
}

<强> PopOverViewController.swift

class PopOverViewController: UIViewController
{
    var completionHandler : ((String?)->(Void))?
    var pickerString = NSArray() as AnyObject as! [String]
    var valueSelected = String()

    @IBAction func pickButton(_ sender: UIButton)
    {
        print("Value: ", valueSelected)

        self.dismiss(animated: true, completion: {[weak self] in
            if let handler = self?.completionHandler
            {
                handler(self?.valueSelected)
            }
        })
    }
}

答案 1 :(得分:0)

如果您想使用代表

添加协议

console.log(foo);

将弱类型委托添加到popOverVC并在dismissViewController

之前使用它
protocol SelectDelegate: NSObjectProtocol {
    func select(_ string: String)
}

将SelectDelegate实现为MainVC

var pickerString = NSArray() as AnyObject as! [String]
var valueSelected = String()
weak var delegate: SelectDelegate?

@IBOutlet weak var picker: UIPickerView!

@IBAction func pickButton(sender: AnyObject?) {
    print("Value: ", valueSelected)

    //added Line
    delegate?.select(valueSelected)
    dismissViewControllerAnimated(true, completion: nil)
}

将popOverVC的委托分配给mainVC

class MainVC {

. . .

    func select(string: String) {
        //doSometing popupVC's string
    }    
}