iOS- Swift:如何使用iOS8中的UIAlertController

1.前言  ide

在前段時間手機QQ:升級iOS8.3後,發圖就崩的狀況,
就是由於iOS8更新UIAlertController後,仍然使用UIAlertview致使的
具體緣由分析 這個能夠看騰訊團隊發出來的總結分享。
 
在Xcode頭文件中蘋果也明確給出用UIAlertController替代UIAlertview和UIActionSheet的標識
 
 
 
因此iOS8之後咱們仍是使用蘋果推薦的UIAlertController吧(這貨竟然是一個ViewController。。)
 

2.如何使用UIAlertController  

2.2.第一種建立方式——默認提示框  

最原始的init通常不用這種,默認是上拉菜單樣式字體

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        // 單擊屏幕觸發
       
       
        //方式一
        var alertVC = UIAlertController()
        alertVC.title = "Title"
        alertVC.message = "Hello,My name Saup"
       
        //由於UIAlertController是控制器,因此咱們如今得改用控制器彈出
        self.presentViewController(alertVC, animated: true, completion: nil)
       
    }

 效果圖1:spa

 

2.2.第二種建立方式——自定義提示框  

UIAlertControllerStyle
UIAlertControllerStyle.Alert        對話框樣式
UIAlertControllerStyle.ActionSheet  上拉菜單樣式
注意第三個參數,要肯定您選擇的是對話框樣式仍是上拉菜單樣式。
 
 
UIAlertActionStyle
經過UIAlertActionStyle,能夠選擇以下三種動做樣式:
常規(default)、取消(cancel)以及警示(destruective)。
UIAlertActionStyle.Default
UIAlertActionStyle.Cancel
UIAlertActionStyle.Destructive // 「警告」樣式會默認把按鈕字體加紅
 
   
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        // 單擊屏幕觸發
       
        //方式二
       
        //建立控制器
        var alertVC = UIAlertController(title: "Title", message: "Please choose!", preferredStyle: UIAlertControllerStyle.ActionSheet)
        //建立按鈕
        var acSure = UIAlertAction(title: "Sure", style: UIAlertActionStyle.Default) { (UIAlertAction) -> Void in
            print("click Sure")
        }
       
        var acCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (UIAlertAction) -> Void in
            print("click Cancel")
        }

//        var acDestuctive = UIAlertAction(title: "Destuctive", style: //UIAlertActionStyle.Destuctive) { (UIAlertAction) -> Void in
//            print("click Destuctive")
//        }
       
        alertVC.addAction(acSure)
        alertVC.addAction(acCancel)
//      alertVC.addAction(acDestuctive)

       
        //由於UIAlertController是控制器,因此咱們如今得改用控制器彈出
        self.presentViewController(alertVC, animated: true, completion: nil)
 
       
    }

效果圖2:3d

 

2.3.第三種建立方式——文本對話框  

  
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        // 單擊屏幕觸發
       
        //方式三
       
        //建立控制器
        var alertVC = UIAlertController(title: "TextFiled", message: "Please input!", preferredStyle: UIAlertControllerStyle.Alert)

        alertVC.addTextFieldWithConfigurationHandler { (tField:UITextField!) -> Void in
           
            tField.placeholder = "Account"

        }
       
       
        alertVC.addTextFieldWithConfigurationHandler {(textField:UITextField!) -> Void in
            textField.placeholder = "Password"
            textField.secureTextEntry = true;
        }
       
        var acOK = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { (alertAction:UIAlertAction!) -> Void in

        }
        var acCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (alertAction:UIAlertAction!) -> Void in
        }
       
        acOK.enabled = false
       
        alertVC.addAction(acOK)
        alertVC.addAction(acCancel)
       
       
       
        //由於UIAlertController是控制器,因此咱們如今得改用控制器彈出
        self.presentViewController(alertVC, animated: true, completion: nil)
    }

效果圖3:code

 

 

 

做者: 清澈Saup
出處: http://www.cnblogs.com/qingche/
本文版權歸做者和博客園共有,歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。blog

相關文章
相關標籤/搜索