情景如下:
最近在做一個項目, 需要自定義一個 彈窗(AlertView) 并且上面需要有一個UITextField.當文本輸入框內容為空時, "取消" 按鈕 和 "兌換" 按鈕 都是灰色的; 當文本框內容不容空時, "兌換"按鈕的顏色變為 綠色.由于iOS目前提倡使用: UIAlertController ,所以就自己摸索使用了一下,果然實現了自己想要的效果. _..... 具體顏色 和 代碼如下
**注意: (1) 要遵守 UITextFieldDelegate 協議, 實現部分協議方法.
(2)其中 CommonTools 這是自己封裝的工具類, 使用者可以不用理睬, 直接用UIColor 替換成自己想要的顏色即可.
(3) 對NSRange 進行擴展,讓swift的string能使用stringByReplacingCharactersInRange. **
============================UIAlertController==========================
樣式
輸入文字前.png
輸入文字后.png
具體代碼
let controller = UIAlertController.init(title: nil, message: "兌換優惠券", 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
// 在這里進行網絡請求, 來驗證
print("\(textField.text!)")
}
//添加文本輸入框
controller.addTextFieldWithConfigurationHandler { (textfield) in
textfield.placeholder = "請輸入優惠碼"
textfield.tintColor = CommonTools.getUIColorFromRGB(0x17c8ce) // 系統綠
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)
判斷文本輸入框的內容變化, 然后"兌換" 做相應的變化
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;
}
對NSRange 進行擴展,讓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 實現方法 =======================
這個也能實現部分功能, 但是不能完全符合自己的要求, 不建議使用
let alertView:UIAlertView = UIAlertView.init(title: "兌換優惠券", message: "", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "兌換")
alertView.alertViewStyle = UIAlertViewStyle.PlainTextInput // 有文本輸入框
let textField:UITextField = alertView.textFieldAtIndex(0)!
textField.placeholder = "請輸入優惠碼"
alertView.show()
UIAlertViewDelegate 協議方法, 來判斷點擊哪個按鈕,處理什么事件
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
print("\(buttonIndex)")
}