Swift4 TextViewとTextField両方あるページの画面スクロール
キーボードで画面が隠れないようにスクロールする機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
エラーメッセージ 2018-05-16 10:02:24.055534+0900 seirenji-iOS[4046:1981423] [Accessibility] ****************** Loading GAX Client Bundle **************** 20300 true 2018-05-16 10:02:29.854909+0900 seirenji-iOS[4046:1981423] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-05-16 10:02:29.857586+0900 seirenji-iOS[4046:1981423] [MC] Reading from public effective user settings. 2018-05-16 10:02:45.530157+0900 seirenji-iOS[4046:1981423] -[seirenji_iOS.SMemoryEditViewController oPickImage:]: unrecognized selector sent to instance 0x10ab08300 2018-05-16 10:02:45.533957+0900 seirenji-iOS[4046:1981423] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[seirenji_iOS.SMemoryEditViewController oPickImage:]: unrecognized selector sent to instance 0x10ab08300' *** First throw call stack: (0x18301ed8c 0x1821d85ec 0x18302c098 0x18d0d2de4 0x1830245c8 0x182f0a41c 0x18cd4e6c8 0x18ce6f8a4 0x18cd5477c 0x18ce8a1dc 0x18cdd1a48 0x18cdc68f8 0x18cdc5238 0x18d5a6c0c 0x18d5a91b8 0x18d5a2258 0x182fc7404 0x182fc6c2c 0x182fc479c 0x182ee4da8 0x184ec7020 0x18cec578c 0x10422bf18 0x182975fc0) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) ### 該当のソースコード // // SMemoryEditViewController.swift // seirenji-iOS // // Created by aki on 2018/05/14. // Copyright © 2018年 aki. All rights reserved. // import Foundation import UIKit import Photos import UserNotifications class SMemoryEditViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { // 1.画像、タイトル、内容が入力 @IBOutlet weak var oSetImage: UIImageView! @IBOutlet weak var oSetTitle: UITextField! @IBOutlet weak var oSetContents: UITextView! @IBOutlet weak var oScrollView: UIScrollView! private var defaultScrollOffset: CGFloat? // var mActiveTextField: UITextField! var pickImage: String? = "" // 画像は文字列に変換して保存 // 1.画像をudで保持するのに最適? 必要なデータタイプは? // 2.変換したデータをprefに登録 // -------------------------------------------------------------------------------- // viewDidLoad() // -------------------------------------------------------------------------------- override func viewDidLoad() { super.viewDidLoad() oSetImage.contentMode = .scaleAspectFit self.oSetContents.delegate = self // oSetContents.delegate = self // oSetTitle.delegate = self } // -------------------------------------------------------------------------------- // didReceiveMemoryWarning() // -------------------------------------------------------------------------------- override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } // -------------------------------------------------------------------------------- // didReceiveMemoryWarning() // -------------------------------------------------------------------------------- override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.defaultScrollOffset = self.oScrollView.contentOffset.y self.registObserveKeyboard() } func showPhotoLibrary() { if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) { let pickerView = UIImagePickerController() pickerView.sourceType = .photoLibrary pickerView.delegate = self self.present(pickerView, animated: true) } } // 5. 写真を選んだ後に呼ばれる処理 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { let image = info[UIImagePickerControllerOriginalImage] as! UIImage oSetImage.image = image self.dismiss(animated: true) } //imageviewをタップしたらカメラロールが開く func oPickImage(_ sender: UIButton) { showPhotoLibrary() let alert: UIAlertController = UIAlertController(title: "表示する画像を選択してください。", message: "", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default) { action in return }) } func textFieldShouldReturn(_ textField: UITextField) -> Bool { oSetTitle.resignFirstResponder() return true } func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if(text == "\n") { oSetContents.resignFirstResponder() return false } return true } // 2.登録ボタン押下でprefに登録 func oContentsRegistration(_ sender: Any) { // PrefManager.memoryImage = self.oSetImage.image! } } extension SMemoryEditViewController: UITextViewDelegate { func registObserveKeyboard() { NotificationCenter.default.addObserver( self, selector: #selector(self.showKeyboard(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil ) NotificationCenter.default.addObserver( self, selector: #selector(self.hideKeyboard(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil ) } func unregistObserveKeyboard() { NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardDidShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardDidHide, object: nil) self.view.endEditing(true) } @objc func showKeyboard(notification: Notification) { // keyboardのサイズを取得 var keyboardFrame: CGRect = CGRect.zero if let userInfo = notification.userInfo { if let keyboard = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue { keyboardFrame = keyboard.cgRectValue } } // 画面サイズの取得 let myBoundSize: CGSize = UIScreen.main.bounds.size if self.oSetContents.frame.size.height <= keyboardFrame.size.height + 60 { self.oScrollView.contentOffset.y = myBoundSize.height / 3 } } @objc func hideKeyboard(notification: Notification) { if let defaultOffset = self.defaultScrollOffset { self.oScrollView.contentOffset.y = defaultOffset } } } Swift4 ソースコード
試したこと
TextViewオンリーだと画面自動スクロールを実装できましたが、混在すると上記のエラーを吐きます。
補足情報(FW/ツールのバージョンなど)
Swift,
Xcode共に2018/05/16時点で最新です。
お願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/17 01:37
2018/05/17 02:35