1
我是swift语言的新手(3.0)。我正在为iOS的新应用程序工作。我怎样才能创建一个文本框,用户键入输入,按下按钮,然后显示按钮下的文本?谢谢!如何显示来自用户输入的文本? Swift 3.0
我是swift语言的新手(3.0)。我正在为iOS的新应用程序工作。我怎样才能创建一个文本框,用户键入输入,按下按钮,然后显示按钮下的文本?谢谢!如何显示来自用户输入的文本? Swift 3.0
您可以使用故事板或以编程方式执行此操作。这两种方法都可以作为的Xcode 8的斯威夫特3
故事板
UITextField
,一个UIButton
和UILabel
从库中拖拽,在您查看控制器UITextField
和UILabel
到您的ViewController.swift
文件。UIButton
创建一个IBAction到同一个ViewController.swift
文件。编写此代码为您的按钮操作功能:
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
}
}
这可能是一个很好的开始:[开始开发iOS应用程序(Swift)](https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/index.html)。 https://developer.apple.com/swift/resources/提供更多资源。 –