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

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

问题描述

几周前,我问了这个问题并得到了非常详细的解释。现在我想将数据传递回第一个ViewController,但我仍然使用相同的方法卡住了。我有一个第一个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:

例如,如果您使用的是Storyboard编辑器:

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天全站免登陆