我是swift语言的新手(3.0)。我正在开发一个适用于iOS的新应用程序。如何创建用户输入输入,按下按钮,然后在按钮下显示文本的文本框?谢谢!
答案 0 :(得分:0)
您可以使用故事板或以编程方式执行此操作。这两个选项都适用于 Xcode 8 Swift 3
<强>故事板强>
UITextField
,UIButton
和UILabel
拖到您的视图控制器上UITextField
和UILabel
ViewController.swift
文件创建一个IBOutlet。 UIButton
文件创建ViewController.swift
的IBAction。 将此代码写入按钮的动作功能中:
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
}
}