質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

5628閲覧

UIAlertController上のUITextFieldの未入力を防ぐ方法

TsksHsgw

総合スコア23

Xcode 7

Xcode 7は、ソフトウェア開発のためのアップルの統合開発環境であるXcodeのバージョン。UIを作成するために用いるグラフィカルツールです。iOS9/OS X El Capitan/watchOS2に対応。Swift 2コンパイラーが搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

1グッド

1クリップ

投稿2016/08/22 18:43

###前提・実現したいこと
質問をご覧いただきまして、ありがとうございます。

現在、UIAlertController上のUITextFieldでinputしてもらうという機能を実装したいと考えております。

そして、このTextFieldが未入力になってしまうのを防ぎたいと考えているのですが、
未入力を防ぐ方法がわからなくて困っております。

これまでに、下記のようなサイトを拝見し、
方法としては、
「テキストフィールドに入力が確認できるまでAlertAction.enabledをfalseにしておく」
という方法が良いのかなと思ったのですが、うまく実装できません。
Check on UIAlertController TextField for enabling the button - StackOverflow
Enable UIAlertAction of UIAlertController only after input - StackOverflow
Objective-c UITextFieldの未入力チェックの罠。

サイトなどはObjective-Cについて書かれたものも参照したのですが、
Swiftの考え方でご回答いただければ幸いです。

よろしくお願い致します。

下記にソースコードを記載致します。

###ソースコード
ViewController.swift

@IBAction func addMemoButtonTapped(sender: AnyObject) { self.navigationController?.setNavigationBarHidden(true, animated: true) let alertController = UIAlertController(title: "新しくアイテムを追加", message: "項目を入力して下さい。", preferredStyle: .Alert) let cancelAction = UIAlertAction(title: "Cancel", style: .Default){ action in print("Cancelが押された!") self.navigationController?.setNavigationBarHidden(false, animated: true) } let otherAction = UIAlertAction(title: "Done", style: .Default){ action in print("Doneが押された!") self.string1 = self.textField1!.text! self.string2 = self.textField2!.text! self.navigationController?.setNavigationBarHidden(false, animated: true) } alertController.addAction(cancelAction) alertController.addAction(otherAction) //textFieldの追加 alertController.addTextFieldWithConfigurationHandler({(textField: UITextField!) -> Void in self.textField1 = textField self.textField1!.tag = 1 self.textField1!.placeholder = "タイトルを記入" }) alertController.addTextFieldWithConfigurationHandler({(textField: UITextField!) -> Void in self.textField2 = textField self.textField2!.tag = 2 self.textField2!.placeholder = "期限を設定" self.textField2!.inputView = self.datePickerView self.textField2!.addTarget(self, action: #selector(ToDoVC.textFieldEditing(_:)), forControlEvents: UIControlEvents.EditingDidBegin) }) presentViewController(alertController, animated: true, completion: nil) }
ikuwow👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下に簡単な例を作成しました、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/22 23:40

編集2016/08/23 11:36
_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

TsksHsgw

2016/08/23 04:35 編集

_Kentarou様 ご回答いただき、誠にありがとうございます。 ご教授いただいた内容でしっかり動作することは確認できたのですが、 2つ目のinputViewにDatePickerを使用しているせいか、 DatePickerを使うとokAction.enable = false が入力後も true になりませんでした。 お手数をおかけして、大変申し訳ないのですが、 もし可能であれば、この原因も併せてご教授いただければ幸いです。 よろしくお願い致します。
_Kentarou

2016/08/23 04:37

そこを見逃していました、すみません。 後で試して追記いたします。
_Kentarou

2016/08/23 11:38

回答にinputViewにdatePickerを使用した例を追記しました。 確認してみてください。
TsksHsgw

2016/08/23 13:25

_Kentarou様 重ね重ね申し訳ございません。 ご回答いただいたコードでしっかり動作を確認することができました。 ご丁寧にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問