swiftSwift程序员

Swift_ios_UIAlertController之text

2016-01-07  本文已影响908人  ChinaSwift




1.UIAlertController的代理函数实现
2.设置监听器传值
3.设置中间变量传值(个人觉得最简洁,最容易理解)

    @IBAction func download(sender: AnyObject) {      
        var textF1 = UITextField()    //设置中间变量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)
    }
    @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消失时移除监听器
             NSNotificationCenter.defaultCenter().removeObserver(self)
        }
        alertC.addTextFieldWithConfigurationHandler { (textField1) -> Void in
            textF1 = textField1
            textF1.placeholder = "hello grandre"
//第一步 在UIAlertController添加UITextField时添加监听器
            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)
    }
//第二步 在控制器中添加监听到事件后的执行方法
   func handleFuntion(notification: NSNotification) {
            let textFied = notification.object as! UITextField
            print("-----\(textFied.text)")
   }
上一篇 下一篇

猜你喜欢

热点阅读