【问题标题】:How can I pass data from a VC to another in Swift 3 without a segue?如何在没有 segue 的情况下在 Swift 3 中将数据从 VC 传递到另一个?
【发布时间】:2016-11-16 16:59:00
【问题描述】:

我在通过 segue 传递数据时遇到问题,因为我正在使用协议来显示我的数据。但我需要找到一种方法将视图控制器中的数据传递给第二个视图控制器。最大的问题是我需要 Swift 3 中的信息,因为我找到的解决方案仅在 Onjective-C 中。

好吧,请忘记我向您展示的示例。我唯一需要做的就是将我的第一个 View Controller 中的 uilabel 内的信息传递给第二个 View Controller 中的另一个 uilabel,但不使用 segue。

如果有人在 Swift 3 中有解决方案,我将不胜感激。

提前致谢!

【问题讨论】:

  • 为什么不能将所需数据设置到ViewControllerB 属性中?正如我在您的示例中看到的,您直接从 ViewControllerA 推送新控制器
  • 我不清楚这里的真正问题是什么?将其转换为 Swift 有困难吗?
  • 是的,这是我的问题,将其转换为 Swift 3!
  • 这是一个扩展示例:code.tutsplus.com/tutorials/…
  • 我按照教程中建议的方式完全按照示例进行操作,但标签没有显示任何内容

标签: ios uiviewcontroller swift3 xcode8


【解决方案1】:

您可以像这样在 Swift 3 中使用 Navigation 传递数据:-

当前班级

 let editProfile = self.storyboard?.instantiateViewController(withIdentifier: "editProfile") as? EditProfileVc
        if detailsModel.count != 0 {
            editProfile?.accModel = detailsModel[0] //Passing data here with the array
        }
 self.navigationController?.pushViewController(editProfile!, animated: true)

和目标类(这里使用模型类变量)

    class EditProfileVc: BaseNotifyM, PickerDelegate {
    
        // Data Variable 
        var accModel:AccountModel? //-- Here you can use [String] or [[String: Any]] or as per you choice for data passing 

      //MARK: - Life Cycle
       override func viewDidLoad() {
        super.viewDidLoad()

        //Do some thing with your data
        print(accModel)
      }

   }

【讨论】:

  • 但是使用 Navigation Controller 的效果与使用 segue 不一样?
  • 顺便问一下,detailsModel 指的是什么?
  • detailsModel 是我传递给下一个 VC 的数据的数组模型。 @Zita Noriega 埃斯特拉达
  • 非常感谢@Anand,但这对我帮助不大。我只需要将 UILabel 中的文本从我的第一个 VC 传递到第二个 VC 中的另一个 UILabel。
猜你喜欢
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2019-01-23
相关资源
最近更新 更多