回答編集履歴

1

修正

2016/08/23 11:36

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -149,3 +149,191 @@
149
149
  }
150
150
 
151
151
  ```
152
+
153
+
154
+
155
+ # 回答追記
156
+
157
+
158
+
159
+
160
+
161
+ ```swift
162
+
163
+
164
+
165
+ import UIKit
166
+
167
+
168
+
169
+ class ViewController: UIViewController, UITextFieldDelegate {
170
+
171
+
172
+
173
+ var okAction: UIAlertAction!
174
+
175
+ var textFieldText1 = ""
176
+
177
+ var textFieldText2 = ""
178
+
179
+ var pikerTextField: UITextField!
180
+
181
+ let datePicker = UIDatePicker()
182
+
183
+
184
+
185
+
186
+
187
+ override func viewDidLoad() {
188
+
189
+ super.viewDidLoad()
190
+
191
+
192
+
193
+ datePicker.datePickerMode = .Date
194
+
195
+ datePicker.addTarget(self, action: #selector(self.textFieldEditing(_:)), forControlEvents: UIControlEvents.ValueChanged)
196
+
197
+
198
+
199
+ }
200
+
201
+
202
+
203
+ func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
204
+
205
+ let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(100 * Double(NSEC_PER_MSEC)))
206
+
207
+ dispatch_after(delayTime, dispatch_get_main_queue()) {
208
+
209
+ self.searchBySearchBarText(textField)
210
+
211
+ }
212
+
213
+ return true
214
+
215
+ }
216
+
217
+
218
+
219
+
220
+
221
+ func searchBySearchBarText(textF: UITextField) {
222
+
223
+ switch textF.tag {
224
+
225
+ case 1: textFieldText1 = textF.text!
226
+
227
+ default: break
228
+
229
+ }
230
+
231
+ okAction.enabled = textFieldText1.characters.count > 0 && textFieldText2.characters.count > 0 ? true : false
232
+
233
+ }
234
+
235
+
236
+
237
+
238
+
239
+ @IBAction func showAlert() {
240
+
241
+
242
+
243
+ let alert = UIAlertController(title:"action",
244
+
245
+ message: "alertView",
246
+
247
+ preferredStyle: UIAlertControllerStyle.Alert)
248
+
249
+
250
+
251
+ let cancelAction = UIAlertAction(title: "Cancel",
252
+
253
+ style: UIAlertActionStyle.Cancel,
254
+
255
+ handler:{
256
+
257
+ (action:UIAlertAction!) -> Void in
258
+
259
+ print("Cancel")
260
+
261
+ })
262
+
263
+ okAction = UIAlertAction(title: "OK",
264
+
265
+ style: UIAlertActionStyle.Default,
266
+
267
+ handler:{
268
+
269
+ (action:UIAlertAction!) -> Void in
270
+
271
+ print("OK")
272
+
273
+ })
274
+
275
+
276
+
277
+ okAction.enabled = false
278
+
279
+ alert.addAction(cancelAction)
280
+
281
+ alert.addAction(okAction)
282
+
283
+
284
+
285
+
286
+
287
+ alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
288
+
289
+ text.delegate = self
290
+
291
+ text.tag = 1
292
+
293
+ text.placeholder = "first textField"
294
+
295
+ })
296
+
297
+
298
+
299
+ alert.addTextFieldWithConfigurationHandler({(text:UITextField!) -> Void in
300
+
301
+ text.delegate = self
302
+
303
+ text.tag = 2
304
+
305
+ text.placeholder = "second textField"
306
+
307
+ self.pikerTextField = text
308
+
309
+ text.inputView = self.datePicker
310
+
311
+ })
312
+
313
+ presentViewController(alert, animated: true, completion: nil)
314
+
315
+ }
316
+
317
+
318
+
319
+
320
+
321
+ func textFieldEditing(datePicker: UIDatePicker) {
322
+
323
+ let formatter = NSDateFormatter()
324
+
325
+ formatter.dateFormat = "yyyy/MM/dd HH:mm"
326
+
327
+ let dateStr = formatter.stringFromDate(datePicker.date)
328
+
329
+ pikerTextField.text = dateStr
330
+
331
+ textFieldText2 = dateStr
332
+
333
+ searchBySearchBarText(UITextField())
334
+
335
+ }
336
+
337
+ }
338
+
339
+ ```