Swift:在视图之间传递多个值

Swift: Pass multiple values between views

我有一个包含两个文本字段和一个按钮的视图。

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

@IBAction func submitUrlButton(sender: AnyObject) {
}

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

var submittedURL = ""
var submittedName = ""

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

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

谢谢

THETOM 编辑:

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 方法。

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

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 的自定义class。

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

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

由于您的视图控制器与 segue 链接,您可以覆盖第一个视图控制器中的 prepareForSegue 方法并通过这样做传递数据

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

    if (segue.identifier == "secondViewController") { // here secondViewController is your segue identifier            
        var secondViewController = segue.destinationViewController as SecondViewController // where SecondViewController is the name of your second view controller class
        secondViewController.submittedURL = inputURL.text
        secondViewController.submittedName = inputName.text
    }
}

并在按钮操作中执行 Segue 使用 perfromSegueWithIdentifier 方法

@IBAction func submitUrlButton(sender: AnyObject) {

    //replace identifier with your identifier from storyboard
    self.performSegueWithIdentifier("secondViewController", sender: self)
}
The simplest way of accessing values globally not neccessary to pass with segue

First View controller

import UIKit
var submittedURL:NSString? // declare them here
var submittedName:NSString? // Now these two variables are accessible globally 
class YourViewController : UIViewController 
{
@IBOutlet var inputURL: UITextField!
@IBOutlet var inputName: UITextField!

@IBAction func submitUrlButton(sender: AnyObject) {
 if inputURL.text == "" && inputName.text == ""
{ 
 //Show an alert here etc
}
else {
 self.submittedURL.text = inputURL.text
self.submittedName.text = inputName.text
 }
}
}

SecondView Controller

import UIKit
class SecondviewController: UIViewController 
{
//inside viewDidload
println(submittedURL)
println(submittedName)
}