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

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

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

Q&A

解決済

1回答

1254閲覧

【swift】 キーボードのリターンボタンが押下されたときに走るdelegateメソッドが実行されない

syosinsya_swift

総合スコア62

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

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

0グッド

0クリップ

投稿2019/09/25 07:43

キーボードのリターンボタンが押下されたときに走るdelegateメソッドが実行されず
原因がわからない状態です。

画面の構成は以下です

イメージ説明

項目Aのグループがタップされた時はピッカーを出して、
項目B,Cのそれぞれのセルがタップされた時はキーボードを出します。

それぞれのクラスセルを宣言しています。

デリゲートは以下で設定しています

// セルのインスタンスを生成する func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == ATable { // 要素のセット // 最初の画面のセルに //UITableViewCellのインスタンスを生成 let cell: ACell = tableView.dequeueReusableCell(withIdentifier: "ACell") as! ACell cell.ALabel.text = "店" cell.alpha = 1 if ACode == "" { cell.APicker.text = "未選択" cell.alpha = 0.6 } return cell } else if tableView == BTable { // 品番セルを生成 let cell: BCell = tableView.dequeueReusableCell(withIdentifier: "BCell") as! BCell cell.BTextField.text = hnbnList[indexPath.row] cell.BTextField.keyboardType = .default cell.BTextField.delegate = cell cell.BTextField.clearButtonMode = .always cell.parentView = self cell.order = indexPath.row cell.BTextField.inputAccessoryView = toolBarText cell.selectionStyle = .none return cell } else { let cell: CCell = tableView.dequeueReusableCell(withIdentifier: String(describing: CCell.self)) as! CCell if indexPath.row == 0 { cell.CLabel.text = "コード1" } else if indexPath.row == 1 { cell.CLabel.text = "コード2" } else { cell.CLabel.text = "パスワード" } cell.alpha = 1 cell.CTextField.keyboardType = .default cell.CTextField.delegate = cell cell.CTextField.clearButtonMode = .always cell.parentView = self cell.order = indexPath.row cell.CTextField.inputAccessoryView = toolBarText cell.selectionStyle = .none return cell } }

Bクラスセル

1 class BCell: UITableViewCell, UITextFieldDelegate { 2 var order: Int = 0 3 var parentView: SettingViewController! 4 5 @IBOutlet weak var BTextField: UITextField! 6 // テキスト編集開始 7 func textFieldDidBeginEditing(_ textField: UITextField) { 8 parentView.moveForEditingTextView(at: order) 9 } 10 // テキスト編集完了 11 func textFieldDidEndEditing(_ textField: UITextField) { 12 parentView.inputBView(at: order, text: self.BTextField.text ?? "") 13 14 parentView.backToOriginScrollPosition() 15 } 16 17 // 編集中 18 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 19 parentView.inputBView(at: order, text: self.BTextField.text ?? "") 20 21 return true 22 } 23 // 削除ボタンが押された場合 24 func textFieldShouldClear(_ textField: UITextField) -> Bool { 25 parentView.deleteBView(at: order) 26 parentView.BTableReload() 27 28 return true 29 } 30 }

Cクラスセル

1 class CCell: UITableViewCell, UITextFieldDelegate { 2 var order: Int = 0 3 var parentView: SettingViewController! 4 5 @IBOutlet weak var CLabel: UILabel! 6 @IBOutlet weak var CTextField: UITextField! 7 8 // テキスト編集開始 9 func textFieldDidBeginEditing(_ textField: UITextField) { 10 parentView.moveForEditingTextView(at: order + parentView.CList.count) 11 } 12 // テキスト編集完了 13 func textFieldDidEndEditing(_ textField: UITextField) { 14 parentView.inputAuthenticationView(at: order + parentView.hnbnList.count, text: self.CTextField.text ?? "") 15 16 parentView.backToOriginScrollPosition() 17 } 18 // 編集中 19 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 20 parentView.inputAuthenticationView(at: order + parentView.CList.count, text: self.CTextField.text ?? "") 21 22 return true 23 } 24 // 削除ボタンが押された場合 25 func textFieldShouldClear(_ textField: UITextField) -> Bool { 26 parentView.deleteCView(at: order) 27 28 return true 29 } 30 31 // returnボタン押下時 32 private func textFieldShouldReturn(textField: UITextField) -> Bool { 33 return true 34 } 35 }

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

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

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

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

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

guest

回答1

0

ベストアンサー

private func textFieldShouldReturn(textField: UITextField) -> Bool {

func textFieldShouldReturn(_ textField: UITextField) -> Bool

投稿2019/09/25 07:52

takabosoft

総合スコア8356

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

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

syosinsya_swift

2019/09/25 07:54

全くその通りでした・・・・・感謝です!
takabosoft

2019/09/25 08:11 編集

一応補足しておくと、UITextFieldDelegate等の定義はSwift1→2→3→4とバージョンアップしている中で何度か変更がおきました。なので、古い記事をコピペすると、実際の関数名とは違ったものになり、それをXcodeが検知し、警告を出してくれると思います(「これUITextFieldDelegateの関数と近いけど微妙に違くね?」的な)。で、その解決策が1つまたは2つ出ると思いますが、その一つが「それはUITextFieldDelegateとは無関係なのでプライベート関数にします」というFIXボタンです(たまに正しい修正方法になるFIXボタンが出ることもあります)。この誤った修正方法のFIXボタンを押した人はもれなく今回のようなバグがでます。Delegateがうまく呼ばれないなと思ったら、①delegateがセットしてあるか ②関数の定義が一字一句有っているか の確認をしてください。②についてはUITextFieldDelegateの定義へジャンプしてコピペしてもってくるのが楽です。
syosinsya_swift

2019/09/25 09:43

ありがとうございます。 注意します
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問