情景如下:
最近在做一個(gè)項(xiàng)目, 需要自定義一個(gè) 彈窗(AlertView) 并且上面需要有一個(gè)UITextField.當(dāng)文本輸入框內(nèi)容為空時(shí), "取消" 按鈕 和 "兌換" 按鈕 都是灰色的; 當(dāng)文本框內(nèi)容不容空時(shí), "兌換"按鈕的顏色變?yōu)?綠色.由于iOS目前提倡使用: UIAlertController ,所以就自己摸索使用了一下,果然實(shí)現(xiàn)了自己想要的效果. _..... 具體顏色 和 代碼如下
**注意: (1) 要遵守 UITextFieldDelegate 協(xié)議, 實(shí)現(xiàn)部分協(xié)議方法.
(2)其中 CommonTools 這是自己封裝的工具類, 使用者可以不用理睬, 直接用UIColor 替換成自己想要的顏色即可.
(3) 對(duì)NSRange 進(jìn)行擴(kuò)展废膘,讓swift的string能使用stringByReplacingCharactersInRange. **
============================UIAlertController==========================
樣式
輸入文字前.png
輸入文字后.png
具體代碼
let controller = UIAlertController.init(title: nil, message: "兌換優(yōu)惠券", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel) { (action) in
}
let OKAction = UIAlertAction.init(title: "兌換", style: UIAlertActionStyle.Default) { (action) in
let textField = (controller.textFields?.first)! as UITextField
// 在這里進(jìn)行網(wǎng)絡(luò)請(qǐng)求, 來(lái)驗(yàn)證
print("\(textField.text!)")
}
//添加文本輸入框
controller.addTextFieldWithConfigurationHandler { (textfield) in
textfield.placeholder = "請(qǐng)輸入優(yōu)惠碼"
textfield.tintColor = CommonTools.getUIColorFromRGB(0x17c8ce) // 系統(tǒng)綠
textfield.delegate = self // 成為代理
}
// 目的是 一開始 "取消" ,"兌換" 按鈕是灰色的
cancelAction.setValue(CommonTools.getUIColorFromRGB(0x999999), forKey: "titleTextColor")
OKAction.setValue(CommonTools.getUIColorFromRGB(0x999999), forKey: "titleTextColor")
controller.addAction(cancelAction)
controller.addAction(OKAction)
self.presentViewController(controller, animated: true, completion: nil)
判斷文本輸入框的內(nèi)容變化, 然后"兌換" 做相應(yīng)的變化
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{
let newText = textField.text!.stringByReplacingCharactersInRange(range.toRange(textField.text!), withString: string)
if newText.length() == 0 {
let alertController = self.presentedViewController as? UIAlertController
if (alertController != nil) {
let okAction = alertController!.actions.last! as UIAlertAction
okAction.setValue(CommonTools.getUIColorFromRGB(0x999999), forKey: "titleTextColor")
}
}else{
let alertController = self.presentedViewController as? UIAlertController
if (alertController != nil) {
let okAction = alertController!.actions.last! as UIAlertAction
okAction.setValue(CommonTools.getUIColorFromRGB(0x17c8ce), forKey: "titleTextColor")
}
}
return true;
}
對(duì)NSRange 進(jìn)行擴(kuò)展繁堡,讓swift的string能使用stringByReplacingCharactersInRange
extension NSRange {
func toRange(string: String) -> Range<String.Index> {
let startIndex = string.startIndex.advancedBy(self.location)
let endIndex = startIndex.advancedBy(self.length)
return startIndex..<endIndex
}
}
========================== UIAlertView 實(shí)現(xiàn)方法 =======================
這個(gè)也能實(shí)現(xiàn)部分功能, 但是不能完全符合自己的要求, 不建議使用
let alertView:UIAlertView = UIAlertView.init(title: "兌換優(yōu)惠券", message: "", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "兌換")
alertView.alertViewStyle = UIAlertViewStyle.PlainTextInput // 有文本輸入框
let textField:UITextField = alertView.textFieldAtIndex(0)!
textField.placeholder = "請(qǐng)輸入優(yōu)惠碼"
alertView.show()
UIAlertViewDelegate 協(xié)議方法, 來(lái)判斷點(diǎn)擊哪個(gè)按鈕,處理什么事件
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
print("\(buttonIndex)")
}