从视图控制器 Xcode 传回数据 [英] Passing data back from view controllers Xcode

查看:20
本文介绍了从视图控制器 Xcode 传回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几周前,我问了这个问题,并得到了非常详细的解释.现在我想将数据传递回第一个 ViewController 但我一直在使用相同的方法卡住.我有第一个 VC 到第二个 VC 的模式,我想在其中编辑一个字符串数组,这些字符串将再次显示在第一个视图中.所以在我的第一个视图中,我有一个数据数组,它应该传递给第二个,因此编辑字段显示当前信息之后,用户必须能够编辑数组的内容并将该数据传递回第一个它显示在标签上.我正在使用 Swift.

A couple of weeks ago I asked this question and got a very detailed explanation. Now I would like to pass data back to the first ViewController but I keep getting stuck using the same method. I have a modal of the first VC to the second, where I would like to edit an array of strings, which will be showed on the first view again. So on my first view I have an array of data, which should be passed to the second so edit fields show the current information after which the user has to be able to edit the contents of the array and pass that data back to the first where it is shown on labels. I'm using Swift.

我的代码:

(在 ViewController.swift 中:)

(in ViewController.swift:)

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        let secondVC = segue.destinationViewController as SecondViewController
        secondVC.namesPlayers = self.namesPlayers
    }

(在 SecondViewController.swift 中:)

(in SecondViewController.swift:)

override func viewDidLoad() {
    super.viewDidLoad()
    labelFirstPlayer.text = namenSpelers[0]
}

谢谢

推荐答案

您需要使用委托.这是一个如何在 Swift 中使用委托的示例.

You need to use a delegate. Here is an example how do use a delegate in Swift.

在您的第一个 ViewController 上,在加载第二个 VC 时设置您的委托:

On your first ViewController, set your delegate when you load the second VC:

例如,如果您使用的是故事板编辑器:

For example, if you are using the Storyboard Editor:

var secondViewController = (segue.destinationViewController.visibleViewController as  MySecondViewControllerClass)
secondViewController.delegate = self

编写一个协议并定义一个函数来写回你的值

Write a Protocol and define a func to write you values back

例如,创建一个名为Protocol.swift"的文件并编写如下内容:

For example, create a file called "Protocol.swift" and write something like that:

protocol writeValueBackDelegate {
    func writeValueBack(value: String)
}

将函数添加到您的 FirstViewController

Add the function to your FirstViewController

func writeValueBack(value: String) {
   // Or any other function you need to transport data
}

还有你的 ViewControllerClass

And to your ViewControllerClass

class ViewController: UIViewController, writeValueBackDelegate

如果您尚未在协议文件中定义的 ViewController 中实现所有方法,则上述行将不起作用.

The above line will not work if you have not implemented all of the methods in ViewController that you defined in your protocol file.

转到第二个视图控制器,并在此处添加委托:

Go to the Second View Controller, and add the delegate here:

class SecondViewController: ViewController {

    // Delegate for FirstViewController
    // Declare as weak to avoid memory cycles
    weak var delegate: writeValueBackDelegate?
}

在您的第二个视图控制器上,您现在可以使用它来调用第一个视图控制器中的 func 并传递数据.

On your Second View Controller, you can now use this to call the func in the first View Controller an pass data.

delegate?.writeValueBack("That is a value")

这篇关于从视图控制器 Xcode 传回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆