swift中UIActionSheet的使用

来源:互联网 发布:阿里巴巴待遇知乎 编辑:程序博客网 时间:2024/04/30 14:15
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 方法1  
  2. let alertSheet = UIActionSheet(title: alertTitle, delegate: nil, cancelButtonTitle: alertOK, destructiveButtonTitle: alertCancel)  
  3. alertSheet.showInView(self.view)  


[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 方法2  
  2. // 实例化时添加代理对象,同时注意添加协议  
  3. let alertSheet = UIActionSheet(title: alertTitle, delegateself, cancelButtonTitle: alertOK, destructiveButtonTitle: alertCancel, otherButtonTitles"警告""提示""通告")  
  4. alertSheet.showInView(self.view)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 添加协议  
  2. class ViewController: UIViewController, UIActionSheetDelegate {  
  3.       
  4.     override func viewDidLoad() {  
  5.         ...  
  6.     }  
  7.     ...  
  8. }  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 代理方法  
  2. // MARK: UIActionSheetDelegate  
  3. func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {  
  4.         let buttonTitle = actionSheet.buttonTitleAtIndex(buttonIndex)  
  5.         if buttonTitle == alertCancel  
  6.         {  
  7.             print("你点击了退出")  
  8.         }  
  9.         else if buttonTitle == alertOK  
  10.         {  
  11.             print("你点击了确定")  
  12.         }  
  13.         else  
  14.         {  
  15.             print("你点击了其他")  
  16.         }  
  17. }  

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 方法3  
  2. // 1 实例化  
  3. let alertSheet = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertControllerStyle.ActionSheet)  
  4. // 2 命令(样式:退出Cancel,警告Destructive-按钮标题为红色,默认Default)  
  5. let cancelAction = UIAlertAction(title: alertCancel, style: UIAlertActionStyle.Cancel, handler: nil)  
  6. let deleteAction = UIAlertAction(title: "删除", style: UIAlertActionStyle.Destructive, handler: nil)  
  7. let archiveAction = UIAlertAction(title: alertOK, style: UIAlertActionStyle.Default, handler: {  
  8.             action in  
  9.             print("OK")  
  10. })  
  11. alertSheet.addAction(cancelAction)  
  12. alertSheet.addAction(deleteAction)  
  13. alertSheet.addAction(archiveAction)  
  14. // 3 跳转  
  15. self.presentViewController(alertSheet, animatedtrue, completion: nil)  



0 0
原创粉丝点击