Swift_ios_UIAlertController之textField傳值

  • 'UIAlertView'在iOS 9.0已經(jīng)廢除,所以我們將以前對(duì)它的使用轉(zhuǎn)移到:UIAlertController上來。

  • UIAlertController完全能夠勝任UIAlertView所需的功能

  • UIAlertController 類不僅用于呈現(xiàn)警告彈窗,還能夠提供 Text Fields 來獲取文本信息輸入。

  • 關(guān)于UIAlertController里如果添加了UITextField的話,在UIAlertController消失之后怎么獲取UITextField的值,應(yīng)該有以下幾種方法。

1.UIAlertController的代理函數(shù)實(shí)現(xiàn)
2.設(shè)置監(jiān)聽器傳值
3.設(shè)置中間變量傳值(個(gè)人覺得最簡潔,最容易理解)

  • 下面將直接奉上N.O.3<設(shè)置中間變量傳值方式>的代碼
    @IBAction func download(sender: AnyObject) {      
        var textF1 = UITextField()    //設(shè)置中間變量textF1  
        let alertC = UIAlertController(title: "Alert Title", message: "Subtitle", preferredStyle: UIAlertControllerStyle.Alert)
        let alertA = UIAlertAction(title: "1", style: UIAlertActionStyle.Default) { (act) -> Void in
            print(textF1.text)
        }
        alertC.addTextFieldWithConfigurationHandler { (textField1) -> Void in
            textF1 = textField1
            textF1.placeholder = "hello grandre"
        }
        alertC.addAction(alertA)
        self.showViewController(alertC, sender: nil)
    }
  • 介紹完<設(shè)置中間變量傳值方式>,再來介紹<設(shè)置監(jiān)聽器傳值>方式
    @IBAction func download(sender: AnyObject) {        
        let alertC = UIAlertController(title: "Alert Title", message: "Subtitle", preferredStyle: UIAlertControllerStyle.Alert)
        let alertA = UIAlertAction(title: "1", style: UIAlertActionStyle.Default) { (act) -> Void in
//第三步 在UIAlertController消失時(shí)移除監(jiān)聽器
             NSNotificationCenter.defaultCenter().removeObserver(self)
        }
        alertC.addTextFieldWithConfigurationHandler { (textField1) -> Void in
            textF1 = textField1
            textF1.placeholder = "hello grandre"
//第一步 在UIAlertController添加UITextField時(shí)添加監(jiān)聽器
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleFuntion:", name: UITextFieldTextDidChangeNotification, object: textField1)
        }        
        alertC.addAction(alertA)
//      self.showViewController(alertC, sender: nil)
        self.presentViewController(alertC, animated: true, completion: nil)
    }
//第二步 在控制器中添加監(jiān)聽到事件后的執(zhí)行方法
   func handleFuntion(notification: NSNotification) {
            let textFied = notification.object as! UITextField
            print("-----\(textFied.text)")
   }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • iOS 8的新特性之一就是讓接口更有適應(yīng)性、更靈活,因此許多視圖控制器的實(shí)現(xiàn)方式發(fā)生了巨大的變化。全新的UIPre...
    烏拉拉zzZ閱讀 945評(píng)論 0 2
  • iOS 8的新特性之一就是讓接口更有適應(yīng)性、更靈活,因此許多視圖控制器的實(shí)現(xiàn)方式發(fā)生了巨大的變化。全新的UIPre...
    Tank丶Farmer閱讀 2,118評(píng)論 2 4
  • { 11、核心動(dòng)畫 需要簽協(xié)議,但是系統(tǒng)幫簽好 一、CABasicAnimation 1、創(chuàng)建基礎(chǔ)動(dòng)畫對(duì)象 CAB...
    CYC666閱讀 1,591評(píng)論 2 4
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,255評(píng)論 4 61
  • 我們班是全校公認(rèn)的“帥美”。從七年級(jí)至九年級(jí),男生變得高打?yàn)t灑、英俊爽朗;女生也是面容精致,天然去雕飾,霸氣...
    SMILing之MN閱讀 204評(píng)論 0 1