以下に簡単な例を作成しました、UITextField
を2つ使用して、両方に入力がない場合OKボタン
が押せなくなっています。
試してみてください。
swift
1import UIKit
2
3class ViewController: UIViewController, UITextFieldDelegate {
4
5 var okAction: UIAlertAction!
6 var textFieldText1 = ""
7 var textFieldText2 = ""
8
9 override func viewDidLoad() {
10 super.viewDidLoad()
11
12 }
13
14 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
15 let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(100 * Double(NSEC_PER_MSEC)))
16 dispatch_after(delayTime, dispatch_get_main_queue()) {
17 self.searchBySearchBarText(textField)
18 }
19 return true
20 }
21
22
23 func searchBySearchBarText(textF: UITextField) {
24 switch textF.tag {
25 case 1: textFieldText1 = textF.text!
26 case 2: textFieldText2 = textF.text!
27 default: break
28 }
29 okAction.enabled = textFieldText1.characters.count > 0 && textFieldText2.characters.count > 0 ? true : false
30
31 }
32
33
34 @IBAction func showAlert() {
35
36 let alert = UIAlertController(title:"action",
37 message: "alertView",
38 preferredStyle: UIAlertControllerStyle.Alert)
39
40 let cancelAction = UIAlertAction(title: "Cancel",
41 style: UIAlertActionStyle.Cancel,
42 handler:{
43 (action:UIAlertAction!) -> Void in
44 print("Cancel")
45 })
46 okAction = UIAlertAction(title: "OK",
47 style: UIAlertActionStyle.Default,
48 handler:{
49 (action:UIAlertAction!) -> Void in
50 print("OK")
51 })
52
53 okAction.enabled = false
54 alert.addAction(cancelAction)
55 alert.addAction(okAction)
56
57
58 alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
59 text.delegate = self
60 text.tag = 1
61 text.placeholder = "first textField"
62 })
63
64 alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
65 text.delegate = self
66 text.tag = 2
67 text.placeholder = "second textField"
68 })
69 presentViewController(alert, animated: true, completion: nil)
70 }
71}
回答追記
swift
1
2import UIKit
3
4class ViewController: UIViewController, UITextFieldDelegate {
5
6 var okAction: UIAlertAction!
7 var textFieldText1 = ""
8 var textFieldText2 = ""
9 var pikerTextField: UITextField!
10 let datePicker = UIDatePicker()
11
12
13 override func viewDidLoad() {
14 super.viewDidLoad()
15
16 datePicker.datePickerMode = .Date
17 datePicker.addTarget(self, action: #selector(self.textFieldEditing(_:)), forControlEvents: UIControlEvents.ValueChanged)
18
19 }
20
21 func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
22 let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(100 * Double(NSEC_PER_MSEC)))
23 dispatch_after(delayTime, dispatch_get_main_queue()) {
24 self.searchBySearchBarText(textField)
25 }
26 return true
27 }
28
29
30 func searchBySearchBarText(textF: UITextField) {
31 switch textF.tag {
32 case 1: textFieldText1 = textF.text!
33 default: break
34 }
35 okAction.enabled = textFieldText1.characters.count > 0 && textFieldText2.characters.count > 0 ? true : false
36 }
37
38
39 @IBAction func showAlert() {
40
41 let alert = UIAlertController(title:"action",
42 message: "alertView",
43 preferredStyle: UIAlertControllerStyle.Alert)
44
45 let cancelAction = UIAlertAction(title: "Cancel",
46 style: UIAlertActionStyle.Cancel,
47 handler:{
48 (action:UIAlertAction!) -> Void in
49 print("Cancel")
50 })
51 okAction = UIAlertAction(title: "OK",
52 style: UIAlertActionStyle.Default,
53 handler:{
54 (action:UIAlertAction!) -> Void in
55 print("OK")
56 })
57
58 okAction.enabled = false
59 alert.addAction(cancelAction)
60 alert.addAction(okAction)
61
62
63 alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
64 text.delegate = self
65 text.tag = 1
66 text.placeholder = "first textField"
67 })
68
69 alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
70 text.delegate = self
71 text.tag = 2
72 text.placeholder = "second textField"
73 self.pikerTextField = text
74 text.inputView = self.datePicker
75 })
76 presentViewController(alert, animated: true, completion: nil)
77 }
78
79
80 func textFieldEditing(datePicker: UIDatePicker) {
81 let formatter = NSDateFormatter()
82 formatter.dateFormat = "yyyy/MM/dd HH:mm"
83 let dateStr = formatter.stringFromDate(datePicker.date)
84 pikerTextField.text = dateStr
85 textFieldText2 = dateStr
86 searchBySearchBarText(UITextField())
87 }
88}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/23 04:35 編集
2016/08/23 04:37
2016/08/23 11:38
2016/08/23 13:25