teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2016/08/23 11:36

投稿

_Kentarou
_Kentarou

スコア8490

answer CHANGED
@@ -73,4 +73,98 @@
73
73
  presentViewController(alert, animated: true, completion: nil)
74
74
  }
75
75
  }
76
+ ```
77
+
78
+ # 回答追記
79
+
80
+
81
+ ```swift
82
+
83
+ import UIKit
84
+
85
+ class ViewController: UIViewController, UITextFieldDelegate {
86
+
87
+ var okAction: UIAlertAction!
88
+ var textFieldText1 = ""
89
+ var textFieldText2 = ""
90
+ var pikerTextField: UITextField!
91
+ let datePicker = UIDatePicker()
92
+
93
+
94
+ override func viewDidLoad() {
95
+ super.viewDidLoad()
96
+
97
+ datePicker.datePickerMode = .Date
98
+ datePicker.addTarget(self, action: #selector(self.textFieldEditing(_:)), forControlEvents: UIControlEvents.ValueChanged)
99
+
100
+ }
101
+
102
+ func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
103
+ let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(100 * Double(NSEC_PER_MSEC)))
104
+ dispatch_after(delayTime, dispatch_get_main_queue()) {
105
+ self.searchBySearchBarText(textField)
106
+ }
107
+ return true
108
+ }
109
+
110
+
111
+ func searchBySearchBarText(textF: UITextField) {
112
+ switch textF.tag {
113
+ case 1: textFieldText1 = textF.text!
114
+ default: break
115
+ }
116
+ okAction.enabled = textFieldText1.characters.count > 0 && textFieldText2.characters.count > 0 ? true : false
117
+ }
118
+
119
+
120
+ @IBAction func showAlert() {
121
+
122
+ let alert = UIAlertController(title:"action",
123
+ message: "alertView",
124
+ preferredStyle: UIAlertControllerStyle.Alert)
125
+
126
+ let cancelAction = UIAlertAction(title: "Cancel",
127
+ style: UIAlertActionStyle.Cancel,
128
+ handler:{
129
+ (action:UIAlertAction!) -> Void in
130
+ print("Cancel")
131
+ })
132
+ okAction = UIAlertAction(title: "OK",
133
+ style: UIAlertActionStyle.Default,
134
+ handler:{
135
+ (action:UIAlertAction!) -> Void in
136
+ print("OK")
137
+ })
138
+
139
+ okAction.enabled = false
140
+ alert.addAction(cancelAction)
141
+ alert.addAction(okAction)
142
+
143
+
144
+ alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
145
+ text.delegate = self
146
+ text.tag = 1
147
+ text.placeholder = "first textField"
148
+ })
149
+
150
+ alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
151
+ text.delegate = self
152
+ text.tag = 2
153
+ text.placeholder = "second textField"
154
+ self.pikerTextField = text
155
+ text.inputView = self.datePicker
156
+ })
157
+ presentViewController(alert, animated: true, completion: nil)
158
+ }
159
+
160
+
161
+ func textFieldEditing(datePicker: UIDatePicker) {
162
+ let formatter = NSDateFormatter()
163
+ formatter.dateFormat = "yyyy/MM/dd HH:mm"
164
+ let dateStr = formatter.stringFromDate(datePicker.date)
165
+ pikerTextField.text = dateStr
166
+ textFieldText2 = dateStr
167
+ searchBySearchBarText(UITextField())
168
+ }
169
+ }
76
170
  ```