Swift 2.0 UIAlertController和UIAlertAction 的使用方法

第一个方法,纯代码建立:spa

 

 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         }

 

 

 

 

效果以下code

 


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

首先建立一个Buttonip

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

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

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

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

要肯定目前的代码界面是RegTableViewController.Swiftclass

如今在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开发,若是有哪里写错的话,但愿各位大大指出来,谢谢,多探讨,多交流,谢谢!

相关文章
相关标签/搜索