gpt4 book ai didi

swift - 如何显示用户输入的文本? swift 3.0

转载 作者:行者123 更新时间:2023-11-28 08:26:25 24 4
gpt4 key购买 nike

我是 swift (3.0) 语言的新手。我正在为 iOS 开发一个新的应用程序。如何创建一个文本框,用户可以在其中输入内容,按下按钮,然后在按钮下方显示文本?谢谢!

最佳答案

您可以使用 Storyboard或以编程方式执行此操作。这两个选项都适用于 Xcode 8 Swift 3

Storyboard

  1. 将库中的 UITextFieldUIButtonUILabel 拖到您的 View Controller 上
  2. UITextFieldUILabel 创建一个 IBOutlet 到您的 ViewController.swift 文件。
  3. 然后为 UIButton 创建一个 IBAction 到同一个 ViewController.swift 文件。
  4. 在按钮的操作函数中编写此代码:

    myLabel.text = myTextField.text

你的整个代码应该是这样的:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myTextField: UITextField!

@IBOutlet weak var myLabel: UILabel!

@IBAction func myButtonPressed(_ sender: AnyObject) {

myLabel.text = myTextField.text

}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

}
}

以编程方式:

import UIKit

class ViewController: UIViewController {

// First create your components

let myTextField: UITextField = {
let textField = UITextField()
textField.backgroundColor = .lightGray //Just so you can see it
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()

let myLabel: UILabel = {
let label = UILabel()
label.text = "Label text" //You may want to set this to something else to start with
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

let myButton: UIButton = {
let button = UIButton()
button.setTitle("Press me", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()

//Then add them as subViews in the viewDidLoad method and set some constraints.

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(myTextField)
myTextField.widthAnchor.constraint(equalToConstant: 250).isActive = true
myTextField.heightAnchor.constraint(equalToConstant: 50).isActive = true
NSLayoutConstraint(item: myTextField, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: myTextField, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: -100).isActive = true

view.addSubview(myLabel)
myLabel.widthAnchor.constraint(equalToConstant: 250).isActive = true
myLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
NSLayoutConstraint(item: myLabel, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: myLabel, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1, constant: 0).isActive = true


view.addSubview(myButton)
myButton.widthAnchor.constraint(equalToConstant: 250).isActive = true
myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
NSLayoutConstraint(item: myButton, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: myButton, attribute: .top, relatedBy: .equal, toItem: myTextField, attribute: .bottom, multiplier: 1, constant: 10).isActive = true

//This sets the function for when the button is pressed
myButton.addTarget(self, action: #selector(ViewController.myButtonPressed), for: .touchUpInside)
}

//Add code here to when the button is pressed
func myButtonPressed() {

myLabel.text = myTextField.text

}

}

关于swift - 如何显示用户输入的文本? swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39838823/

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