UIAlertView/UIActionSheet
UIAlertView
//一个按钮的提醒 @IBAction func oneButtonAler() { //创建单一按钮提醒视图 let oneButtonAlert:UIAlertView = UIAlertView(title: "提醒", message: "这是一个简单的提醒视图", delegate: nil, cancelButtonTitle: "确定") //显示提醒视图 oneButtonAlert.show() //设置提醒视图样式 oneButtonAlert.alertViewStyle = UIAlertViewStyle.LoginAndPasswordInput //来获取提醒视图上的文本框 var textField : UITextField? = oneButtonAlert.textFieldAtIndex() //设置文本框占位符 textField?.placeholder = "请输入账号" } //多个按钮的提醒 @IBAction func moreButtonAler() { //创建多按钮提醒视图 let moreButtonAlert:UIAlertView = UIAlertView(title: "提醒", message: "多按钮提醒钮视图,请选择一个按钮", delegate: self, cancelButtonTitle: "确定" , otherButtonTitles: "按钮 1","按钮 2","按钮 3","按钮 4") //显示图像视图 moreButtonAlert.show() //设置代理 moreButtonAlert.delegate = self // //方法二 // //初始化空得alert // let alert = UIAlertView() // //设置标题 // alert.title = "提醒" // //设置代理 // alert.delegate = self // //设置提醒信息 // alert.message = "多按钮提醒钮视图,请选择一个按钮" // // //添加按钮,可以添加多个 // alert.addButtonWithTitle("按钮 1") // alert.addButtonWithTitle("按钮 2") // alert.addButtonWithTitle("按钮 3") // alert.addButtonWithTitle("按钮 4") // // //显示 // alert.show() } override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ // MARK: - UIAlertViewDelegate //根据被点击按钮的索引处理点击事件 func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { switch buttonIndex { : println("按钮 1") : println("按钮 2") : println("按钮 3") : println("按钮 4") default: println("点击确定") } } //取消按钮的事件 func alertViewCancel(alertView: UIAlertView) { } //即将显示时 func willPresentAlertView(alertView: UIAlertView) { } //已经显示时的事件 func didPresentAlertView(alertView: UIAlertView) { } //即将消失时的事件 func alertView(alertView: UIAlertView, willDismissWithButtonIndex buttonIndex: Int) { } //已经消失时的事件 func alertView(alertView: UIAlertView, didDismissWithButtonIndex buttonIndex: Int) { } //编辑任何默认的字段添加的风格之后调用 func alertViewShouldEnableFirstOtherButton(alertView: UIAlertView) -> Bool { return false }
UIActionSheet
@IBAction func showActionSheet1() { //创建不带红色按钮的操作表 var actionSheet:UIActionSheet = UIActionSheet(title: "请选择分享方向", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "邮件", "短信","微博","微信") actionSheet.delegate = self //显示到self.view视图上 actionSheet.showInView(self.view) //设置标题 actionSheet.title = "新标题" //设置样式 actionSheet.actionSheetStyle = UIActionSheetStyle.BlackTranslucent //添加按钮 actionSheet.addButtonWithTitle("新加按钮") //根据index坐标获取一个按钮的文本 var butIndex = actionSheet.buttonTitleAtIndex() //获取取消按钮的坐标 var cancelIndex = actionSheet.cancelButtonIndex //获取按钮个数 var butCount = actionSheet.numberOfButtons } @IBAction func showActionSheet2() { //创建带有红色按钮的操作表 var actionSheet2:UIActionSheet = UIActionSheet(title: "将删除该条评论", delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: "确定删除") //显示到self.view视图上 actionSheet2.showInView(self.view) } override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ // MARK: - UIActionSheetDelegate //根据被点击按钮的索引处理点击事件 func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) { switch buttonIndex { : println("点击取消") : println(") : println(") : println(") default: println("未知") } } //选择取消按钮 func actionSheetCancel(actionSheet: UIActionSheet) { } //即将显示时 func willPresentActionSheet(actionSheet: UIActionSheet) { } //已显示时 func didPresentActionSheet(actionSheet: UIActionSheet) { } //即将消失 func actionSheet(actionSheet: UIActionSheet, willDismissWithButtonIndex buttonIndex: Int) { } //已消失 func actionSheet(actionSheet: UIActionSheet, didDismissWithButtonIndex buttonIndex: Int) { }