
30
|
第
1
章
@IBAction func showAlert(sender: AnyObject) {
}
4.
在前面创建的
showAlert
方法中添加下述代码:
var alert = UIAlertController(title: "Hello!", message: "Hello, world!",
preferredStyle: UIAlertControllerStyle.Alert)
❶
alert.addAction(UIAlertAction(title: "Close",
style: UIAlertActionStyle.Default, handler: nil))
❷
self.presentViewController(alert, animated: true, completion: nil)
❸
self.helloButton.setTitle("Test!", forState: UIControlState.Normal)
❹
这些代码的作用是:
❶
创建一个
UIAlertController
实例,在弹出的窗口中显示一个消息。这个窗口的
标题是“
Hello!
”,里面的文本是“
Hello, world!
”。
❷
添加关闭提示框的操作,文本是“
Close
”。
❸
向用户显示提示框。
❹
最后,把按钮的标题设为“
Test!
”。
现在,应用可以运行了。单击左上角的运行按钮,应用会在
iPhone
模拟器中启动。
首次启动应用时要等一会儿,别着急。首次启动模拟器可能要用相当长的时间。 ...