gpt4 book ai didi

ios - 以编程方式在 View Controller 之间传递数据 Swift

转载 作者:行者123 更新时间:2023-11-28 09:33:56 25 4
gpt4 key购买 nike

我有两个 View Controller 类。 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
}

}

//主视图 Controller 类

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



}

最佳答案

你需要遵循一些步骤

第一步

最初将您的初始 VC 嵌入到导航 Controller 中,例如

Now select the first controller in Storyboard and click on Editor > Embed in... > Navigation Controller.

enter image description here

第二步

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

Select the yellow circle at the top of the controller, click on the Identify inspector panel icon on the right side of the XCode window, and type the name of your new .swift file in the Class and StoryboardID fields

例如

enter image description here

第三步

传递字符串

Now select the other controller in Storyboard and add this variable right below the SecondViewController class declaration:

 class SecondViewController: UIViewController {


let secondLabel = UILabel()

var stringPassed = ""

Make the app assign the value of this variable to secondLabel with the following line of code in the viewDidLoad() method

第四步

在你的第一个 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)

}

传递字符串

Now select the other controller in Storyboard and add this variable right below the SecondViewController class declaration:

 class SecondViewController: UIViewController {


let secondLabel = UILabel()

var stringPassed = ""

Make the app assign the value of this variable to secondLabel with the following line of code in the viewDidLoad() method

函数 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)
}

关于ios - 以编程方式在 View Controller 之间传递数据 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42608097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com