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

Qui*_*let 0 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 {
Run Code Online (Sandbox Code Playgroud)

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

window?.makeKeyAndVisible()

window?.rootViewController = MainViewController()

window?.backgroundColor = UIColor.yellow

application.statusBarStyle = .lightContent


return true
  }

 }
Run Code Online (Sandbox Code Playgroud)

// 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
    }

}
Run Code Online (Sandbox Code Playgroud)

// 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)
  }



}
Run Code Online (Sandbox Code Playgroud)

Anb*_*hik 6

你需要遵循一些步骤

步骤1

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

现在选择Storyboard中的第一个控制器,然后单击Editor> Embed in ...> Navigation Controller.

在此输入图像描述

第2步

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

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

例如

在此输入图像描述

第三步:

传递一个字符串

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

 class SecondViewController: UIViewController {


let secondLabel = UILabel()

  var stringPassed = ""
Run Code Online (Sandbox Code Playgroud)

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

第4步

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

func buttonTarget() {

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

    navigationController?.pushViewController(myVC, animated: true)

}
Run Code Online (Sandbox Code Playgroud)

最后你得到了输出

 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)
  }
Run Code Online (Sandbox Code Playgroud)

您可以在此处获取教程

更新XIB

   func buttonTarget() {

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


}
Run Code Online (Sandbox Code Playgroud)

没有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
 }
Run Code Online (Sandbox Code Playgroud)

在MainViewController上

 func buttonTarget() {

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

 }
Run Code Online (Sandbox Code Playgroud)

传递一个字符串

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

 class SecondViewController: UIViewController {


let secondLabel = UILabel()

  var stringPassed = ""
Run Code Online (Sandbox Code Playgroud)

使应用程序使用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)
  }
Run Code Online (Sandbox Code Playgroud)