ios - 通过 unwind segue 传递数据

标签 ios swift segue

我一直在寻找这个问题的答案......我不知道如何通过 unwind segue 传递数据。我知道我应该使用 prepareForSegue 但我不确定我应该输入什么。我正在尝试从一个类中的 UIPickerView 传递一个变量 (myLabel):

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    myLabel.text = pickerData[row]
}

到另一个类(class)。我已经有了 unwind segue 代码,但不知道将 prepareForSegue 放在哪里以及放什么。

如果我的问题需要更多说明,请告诉我。

最佳答案

如果我正确理解你的问题,你应该这样做:

class MyPickerController : UIViewController {
  @IBOutlet weak var myLabel: UILabel!

  var pickerData:[String]

  func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    myLabel.text = pickerData[row]
    performSegueWithIdentifier( "mySegueName", sender: self )
  }

  func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let myController = segue.destinationController as? MyDestinationViewController {
      myController.text = myLabel.text
  }
}

这会将标签文本传递给 MyDestinationViewController 实例。 请注意,我没有传递 UILabel 对象,而是文本。 您不能将该 UILabel 对象放置在目标 Controller 界面中,但您可以使用文本并为其设置另一个标签,例如。

关于ios - 通过 unwind segue 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30622999/

相关文章:

ios - 与 SDWebImage 相关的架构 arm64 的重复符号

ios - Swift 3.0 中的结构体枚举

ios - 交互式过渡 : Segue declaration

swift - 如何从 UICollectionView 中的多个部分传递数据

ios - UICollectionViewCell 大小适合设备旋转时的所有单元格

ios - swift -Xcode : performSegue in if statement

ios - 使用自定义 init 转入 UITableViewController

objective-c - 如何在 Objective-C 中执行 const char 的 XOR?

ios - 应用内购买 : sorting array of product identifiers

ios - 如何在 SwiftUI 中使用渐变填充形状