【发布时间】:2016-02-14 22:16:29
【问题描述】:
我在 Swift 中的第一个代码有一些问题,当我运行它时,它看起来很好,但它显示了例如Hello 可选“(name)”而不是 Hello (name)。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var helloLabel: UILabel!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var sayHelloButton: UIButton!
@IBAction func sayHelloAction(sender: AnyObject)
{
let name = nameTextField.text
if name!.isEmpty {
let alert = UIAlertController(title: "Error", message: "Please enter a name", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
helloLabel.text = "Hello \(name)!"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupUI() {
helloLabel.text = "Hello There!"
helloLabel.textColor = UIColor.blueColor()
helloLabel.textAlignment = NSTextAlignment.Center
nameTextField.placeholder = "Enter your name"
sayHelloButton.setTitle("Say Hello", forState: .Normal)
}
}
有人可以帮帮我吗?
Doms。
【问题讨论】:
-
我知道阅读文档并不酷,但至少阅读 Swift 语言指南的 The Basics 章节是值得的。
-
您能否更新您的问题以包括您刚刚尝试过的内容以及新崩溃发生的位置?我还建议首先注释掉与警报相关的代码,然后只做一个
print()语句,以便更轻松地找出问题所在。 -
@vadian 谢谢,但我已经准备好了 :)