gpt4 book ai didi

ios - Xcode Swift 将 String 从 popover View 传递给主 VC

转载 作者:可可西里 更新时间:2023-11-01 02:12:37 26 4
gpt4 key购买 nike

我试图将数据从我的 popOverViewController 中的字符串传递回我的 mainVC 作为字符串。但我不知道该怎么做,所以请帮助任何人。

我的 mainVC segue 函数:

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)
}

need to send valueSelected string back to previous navigated view.

最佳答案

为此,您可以在 swift 中使用闭包。以下是如何做到这一点:

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)
}
})
}
}

关于ios - Xcode Swift 将 String 从 popover View 传递给主 VC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40562579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com