Swift:在视图之间传递多个值 [英] Swift: Pass multiple values between views

查看:89
本文介绍了Swift:在视图之间传递多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两个文本字段和一个按钮的视图.

I have a view that has two text fields and a button.

@IBOutlet var inputURL: UITextField!
@IBOutlet var inputName: UITextField!

@IBAction func submitUrlButton(sender: AnyObject) {
}

和具有两个变量的第二个视图:

and a second view that has two variables:

var submittedURL = ""
var submittedName = ""

println("Name \(submittedName)")
println("URL \(submittedURL)")

在Swift中,如何传递在两个文本字段中输入的值并将它们分配给第二个视图中的那些变量?

In Swift How do I pass the values entered in the two text fields and assign them to those variables in the second view?

谢谢

编辑主题:

import UIKit

class ViewController: UIViewController {

@IBOutlet var inputURL: UITextField!

@IBAction func submitBtn(sender: AnyObject) {
    performSegueWithIdentifier("submissionSegue", sender: self)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    // Create a new variable to store the instance of the next view controller
    let destinationVC = segue.destinationViewController as BrandsViewController
    destinationVC.submittedURL.text = inputURL.text
}
}

推荐答案

您可以使用prepareForSegue方法.

You can use the method prepareForSegue.

在第一个视图(来自segue的视图)中,编写以下代码:

In the first view (the one from which the segue is coming from) write the following code :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    // Create a new variable to store the instance of the next view controller 
    let destinationVC = segue.destinationViewController as CustomViewController
    destinationVC.submittedURL = inputURL.text
    destinationVC.submittedName = inputName.text
}

这是CustomViewController,是segue要转到的UIViewController的自定义类.

Here CustomViewController is the custom class of the UIViewController to which the segue is going to.

要在您的按钮@IBAction中以编程方式执行segue,请执行以下操作:

To perform the segue programmatically in your button @IBAction do that :

@IBAction func buttonWasClicked(sender: AnyObject) {
    performSegueWithIdentifier("submissionSegue", sender: self)
}

这篇关于Swift:在视图之间传递多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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