Swift 2.0 UIAlertController和UIAlertAction 的使用方法

第一个方法,纯代码创建:

 

 1 self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "ButtonTips") 
 2 func ButtonTips(){
 3 
 4          let a = UIAlertController(title: "注册成功", message: "您已经注册成功,请返回登陆", preferredStyle: UIAlertControllerStyle.Alert)         
 5          let b = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
 6          a.addAction(b)
 7          self.presentViewController(a, animated: true, completion: nil)   
 8  
 9 
10         }

 

 

 

 

效果如下

 


 第二个方法,故事板Storyboard创建:

首先创建一个Button

我这里选择的是Done(返回)

为控制器添加一个cocoa,我这里的cocoa为UITableViewController,cocoa名为RegTableViewController。

(貌似看图比较简单,不过我刚自学两个月,感觉很苦逼,觉得有必要都写全)

然后打开show the Assistant editor,也就是两个圆圈那个图标,加载一个代码界面

要确定目前的代码界面是RegTableViewController.Swift

现在在Done这个Button图标右键拖拉到旁边的代码里面

我这里是已经拖了,如果没有拖的话,代码界面是一条横线。

我这里给这个控件命名为Tips,方式为Action。

好,现在返回代码界面

在控件下面直接给定义

1     @IBAction func Tips(sender: AnyObject) {//左边的那些你已经有了
2     
3         let ButtonTips = UIAlertController(title: "注册成功", message: "您已经注册成功,请返回登陆", preferredStyle: UIAlertControllerStyle.Alert)
4         let action = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
5         ButtonTips.addAction(action)
6         self.presentViewController(ButtonTips, animated: true, completion: nil)
7
8 }//左边的}你已经有了

 

运行的效果图跟方法一一样

0基础刚学Swift开发,如果有哪里写错的话,希望各位大大指出来,谢谢,多探讨,多交流,谢谢!

posted @ 2015-10-25 19:33  GhostBug  阅读(531)  评论(0编辑  收藏  举报