以编程方式Swift在视图控制器之间传递数据

时间:2017-03-05 11:58:32

标签: ios swift swift3

我有两个viewcontroller类。 MainViewController有一个UITextField,而SecondViewController有一个UILabel。我想从UILabe中的UITextField打印文本。请告诉我以编程方式执行此操作的最佳方法。以下是我的代码:

// AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?


  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

window = UIWindow(frame:UIScreen.main.bounds)

window?.makeKeyAndVisible()

window?.rootViewController = MainViewController()

window?.backgroundColor = UIColor.yellow

application.statusBarStyle = .lightContent


return true
  }

 }

// MainViewController类

import UIKit

class MainViewController: UIViewController {


  let label = UILabel()

  let textField = UITextField()



  override func viewDidLoad() {

    super.viewDidLoad()


    view.backgroundColor = UIColor.gray
    setupLabel()
    setupTextField()
    setupButton()
  }

  func setupLabel() {

    label.frame = CGRect(x: 40, y: 80, width: 300, height: 60)
    label.text = "welcome to my world"
    label.textColor = UIColor.yellow
    label.font = UIFont.boldSystemFont(ofSize: 25)
    label.textAlignment = .center
    label.layer.borderWidth = 2
    label.layer.borderColor = UIColor.yellow.cgColor
    label.layer.cornerRadius = 5
    view.addSubview(label)
  }

  func setupTextField() {

    textField.frame = CGRect(x: 10, y: 200, width: self.view.frame.size.width - 20, height: 60)
    textField.placeholder = "text here"
    textField.textAlignment = .center
    textField.font = UIFont.systemFont(ofSize: 25)
    textField.layer.borderWidth = 2
    textField.layer.borderColor = UIColor.yellow.cgColor
    textField.layer.cornerRadius = 5
    view.addSubview(textField)
  }

  func setupButton() {

    let button = UIButton()
    button.frame = CGRect(x: 50, y: 300, width: self.view.frame.size.width - 100, height: 60)
    button.setTitle("Enter", for: .normal)
    button.setTitleColor(UIColor.yellow, for: .normal)
    button.layer.borderWidth = 2
    button.layer.borderColor = UIColor.yellow.cgColor
    button.layer.cornerRadius = 5
    button.addTarget(self, action: #selector(buttonTarget), for: .touchUpInside)
    view.addSubview(button)
  }

  func buttonTarget() {

    // i missed here maybe
    }

}

// SecondViewController类

import UIKit

class SecondViewController: UIViewController {


    let secondLabel = UILabel()

  override func viewDidLoad() {

    super.viewDidLoad()

    setupLabelSecond()
  }


  func setupLabelSecond() {

    secondLabel.frame = CGRect(x: 40, y: 80, width: 300, height: 60)
    secondLabel.text = "this is Second Page"
    secondLabel.textColor = UIColor.yellow
    secondLabel.font = UIFont.boldSystemFont(ofSize: 25)
    secondLabel.textAlignment = .center
    secondLabel.layer.borderWidth = 2
    secondLabel.layer.borderColor = UIColor.yellow.cgColor
    secondLabel.layer.cornerRadius = 5
    view.addSubview(secondLabel)
  }



}

2 个答案:

答案 0 :(得分:6)

你需要遵循一些步骤

<强>步骤1

最初将您的初始VC嵌入导航控制器,例如

  

现在选择Storyboard中的第一个控制器,然后单击编辑器&gt;嵌入...&gt;导航控制器。

enter image description here

<强>步骤2

现在你必须将Storyboard中的第二个Controller与你的新SecondViewController.swift文件链接起来。

  

选择控制器顶部的黄色圆圈,单击XCode窗口右侧的识别检查器面板图标,然后在Class和StoryboardID字段中键入新.swift文件的名称

例如

enter image description here

<强>步骤3

传递字符串

  

现在在Storyboard中选择另一个控制器,并在SecondViewController类声明的正下方添加此变量:

 class SecondViewController: UIViewController {


let secondLabel = UILabel()

  var stringPassed = ""
  

让应用程序使用 viewDidLoad()方法中的以下代码行将此变量的值分配给secondLabel

<强>步骤4

在你的第一个VC上,在按钮内

func buttonTarget() {

    let myVC = storyboard?.instantiateViewControllerWithIdentifier("SecondVC") as! SecondViewController
    myVC.stringPassed = label.text!

    navigationController?.pushViewController(myVC, animated: true)

}

最后你得到了输出

 func setupLabelSecond() {

    secondLabel.frame = CGRect(x: 40, y: 80, width: 300, height: 60)

    if let outputText =  stringPassed
    {
    secondLabel.text = outputText
    }else
     {
      secondLabel.text = "this is Second Page"
     }
    secondLabel.textColor = UIColor.yellow
    secondLabel.font = UIFont.boldSystemFont(ofSize: 25)
    secondLabel.textAlignment = .center
    secondLabel.layer.borderWidth = 2
    secondLabel.layer.borderColor = UIColor.yellow.cgColor
    secondLabel.layer.cornerRadius = 5
    view.addSubview(secondLabel)
  }

您可以获得示例教程here

更新XIB

   func buttonTarget() {

    var vcPass = SecondViewController(nibName: "SecondViewController", bundle: nil)
     vcPass.stringPassed = label.text!
    self.navigationController?.pushViewController(vcPass, animated: true)


}

在没有XIB和Storboard的情况下更新

更改您的appdelegate

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    // Override point for customization after application launch.

    let navigation = UINavigationController(rootViewController: MainViewController())
    self.window?.rootViewController = navigation
    self.window?.makeKeyAndVisible()
    return true
 }
MainViewController上的

 func buttonTarget() {

     var vcPass = SecondViewController()
     vcPass.stringPassed = label.text!
    self.navigationController?.pushViewController(vcPass, animated: true)

 }

传递字符串

  

现在在Storyboard中选择另一个控制器,并在SecondViewController类声明的正下方添加此变量:

 class SecondViewController: UIViewController {


let secondLabel = UILabel()

  var stringPassed = ""
  

让应用程序使用 viewDidLoad()方法中的以下代码行将此变量的值分配给secondLabel

func setupLabelSecond(){

    secondLabel.frame = CGRect(x: 40, y: 80, width: 300, height: 60)

    if let outputText =  stringPassed
    {
    secondLabel.text = outputText
    }else
     {
      secondLabel.text = "this is Second Page"
     }
    secondLabel.textColor = UIColor.yellow
    secondLabel.font = UIFont.boldSystemFont(ofSize: 25)
    secondLabel.textAlignment = .center
    secondLabel.layer.borderWidth = 2
    secondLabel.layer.borderColor = UIColor.yellow.cgColor
    secondLabel.layer.cornerRadius = 5
    view.addSubview(secondLabel)
  }

答案 1 :(得分:2)

我知道两种可能的方法。

1)将初始视图控制器嵌入导航控制器中。  在第二个视图中创建一个var。例如

var x = ""

在第一个视图中

创建文本字段出口和第二个视图控制器的对象

@IBOutlet weak var enteredName: UITextField!

let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController

然后,将输入的文本字段通过该对象分配给变量x。

secondVC.x = enteredName.text!

在第二个vc中,您可以将x的值分配给您的标签。

mylabel.text = x

<强> ____________________________________________________________________

第二种方法是使用用户默认值。

获取用户输入并将其设置为用户默认值。

 let defaults = UserDefaults.standard   
defaults.set(self.enteredName.text!,forKey: "userName")
    defaults.synchronize()

并在第二个视图中使用

let defaults = UserDefaults.standard  
yourlabel.text = defaults.object(forKey: "userName")