您的位置:首页 > 移动开发 > Swift

Ray Wenderlich的swift教程03--提醒视图控制器UIAlertController

2015-08-12 20:52 645 查看
tutorial4

本课程是实现一个在30秒内点击按钮数统计的小游戏。

添加一个Lable到故事板上,编辑文本后,选菜单Editor\Size to Fit(或按command+=),使得文本框自适应文本大小。

添加一个定时器var timer = NSTimer(),在didView里调用setupGame,就能实现启动后,开始30秒倒计时

func setupGame(){
count = 0
seconds = 30
ScoresLable.text = "Score:\n\(count)"
timerLabel.text = "Time:\(seconds)"
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
}
func subtractTime() {
seconds--
timerLabel.text = "Time: \(seconds)"

if(seconds == 0)  {
//stop timer
timer.invalidate()
}
}

在timer.invalidate()后面,添加以下句子

创建一个UIAlertController类的alert,

let alert = UIAlertController(title: "Time is up!",
message: "You scored \(count) points",
preferredStyle: UIAlertControllerStyle.Alert)


接着添加一个BUTTON用addAction(),如果点击这个按钮,回到setupGame()函数,再玩一次游戏

alert.addAction(UIAlertAction(title: "Play Again", style: UIAlertActionStyle.Default, handler: {
action in self.setupGame()
}))

最后显示这个alert的视图控件

presentViewController(alert, animated: true, completion:nil)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: