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

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

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

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

Q&A

解決済

1回答

844閲覧

ビルド後、TextFieldをタップするとエラー[unrecognized selector sent to instance]になる

yukky05

総合スコア11

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

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

0グッド

0クリップ

投稿2017/10/01 22:27

プログラミング初心者です。

ScrollViewの上に、TextFieldを設置し、タップすると、TextFieldがキーボードに隠れないようにの高さ調整するための実装をし、特にエラー表示もなかったのでビルドし、該当のTextFieldをシュミレーターでタップしたところ、下記のようなエラーが表示されました。
こちらの解決方法がわからないため、ご教示いただけると幸いです。

なお事前に、以下の類似のエラーかと思いIBOutletやプロパティ名を付け直したりしましたが、エラーは解消しませんでした。

https://qiita.com/ruwatana/items/7cfc63ce102938962260

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[fbtest.LoginViewController handleKeyboardWillShowNotification:]: unrecognized selector sent to instance 0x786611b0'

swift3.0

1import UIKit 2import Firebase 3 4class LoginViewController: UIViewController, UITextFieldDelegate, UIScrollViewDelegate { 5 6 7 @IBOutlet weak var loginScrollView: UIScrollView! 8 9 @IBOutlet weak var emailTextField: UITextField! 10 11 @IBOutlet weak var passwordTextField: UITextField! 12 13 14 var txtActiveField = UITextField() 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 emailTextField.delegate = self 19 passwordTextField.delegate = self 20 self.title = "ログイン" 21 22 } 23 24 override func didReceiveMemoryWarning() { 25 super.didReceiveMemoryWarning() 26 } 27 28 //Keyboard以外をタッチで編集を終了 29 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 30 self.view.endEditing(true) 31 } 32 33 //リターンキーを押すと編集を終了 34 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 35 textField.resignFirstResponder() 36 return true 37 } 38 39 //テキストフィールド編集開始/終了時の処理 40 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 41 txtActiveField = textField 42 return true 43 } 44 45 func textFieldShouldClear(_ textField: UITextField) -> Bool { 46 self.view.endEditing(true) 47 return true 48 } 49 50 51 //keyboardを押した時の高さを取得しスクロールする処理 52 func handleKeyboardWillShowNotification(notification: NSNotification) { 53 54 let userInfo = notification.userInfo! 55 let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 56 let myBoundSize: CGSize = UIScreen.main.bounds.size 57 58 let txtLimit = txtActiveField.frame.origin.y + txtActiveField.frame.height + 8.0 59 let kbdLimit = myBoundSize.height - keyboardScreenEndFrame.size.height 60 61 print("テキストフィールドの下辺:(txtLimit)") 62 print("キーボードの上辺:(kbdLimit)") 63 64 if txtLimit >= kbdLimit { 65 loginScrollView.contentOffset.y = txtLimit - kbdLimit 66 } 67 } 68 69 //keyboardを閉じるときに高さを元に戻す処理 70 func handleKeyboardWillHideNotification(notification: NSNotification) { 71 loginScrollView.contentOffset.y = 0 72 } 73 74 //UITextFieldの下辺とキーボードの上辺が重なっているかどうか調べるための処理 75 //NotificationCenterの設定周り 76 override func viewWillAppear(_ animated: Bool) { 77 super.viewWillAppear(animated) 78 79 let notificationCenter = NotificationCenter.default 80 notificationCenter.addObserver(self, selector: Selector(("handleKeyboardWillShowNotification:")), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 81 notificationCenter.addObserver(self, selector: Selector(("handleKeyboardWillHideNotification:")), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 82 } 83 84 override func viewDidDisappear(_ animated: Bool) { 85 super.viewDidDisappear(animated) 86 87 let notificationCenter = NotificationCenter.default 88 notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 89 notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 90 } 91

どうぞよろしくお願いいたします。

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

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

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

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

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

fuzzball

2017/10/02 00:52 編集

(deleted)
guest

回答1

0

ベストアンサー

その1

swift

1func handleKeyboardWillShowNotification(_ notification: NSNotification) {

Selector()による指定では最初のラベルを付けられませんので、アンダーバーを付けてラベル無しにします。(Objective-C形式のシグネチャだからだと思います)

その2(おすすめ!)

#selector()を使います。

swift

1notificationCenter.addObserver(self, selector: #selector(handleKeyboardWillShowNotification(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)

投稿2017/10/02 00:51

fuzzball

総合スコア16731

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

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

yukky05

2017/10/02 15:30

ありがとうございます!オススメの解決方法で無事動作しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問