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

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

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

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

Swift

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

Q&A

解決済

1回答

1323閲覧

テキストビューのUITapGestureRecognizerを有効にしたい。(swift)

off927

総合スコア2

Xcode

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

Swift

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

0グッド

0クリップ

投稿2020/05/23 13:16

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
(例)swiftで勉強用にアプリを作成しております。
親ビューの+ボタンをクリックすると新規の子ビューが作られるというアプリです。子ビューはボタン、ラベル、テキストビューで構成されており、テキストビュー内のテキストは編集可能にしたいと思っていました(「ここはセリフの部分です。」がテキストビュー)。子ビューの定義はViewController.swiftとは別のCustomView.swiftで定義してます。
イメージ説明

発生している問題・エラーメッセージ

テキストビューを編集する際、対象のテキストビューをクリックすると、キーボードが表示されるよう、CustomView.swiftにUITapGestureRecognizerを定義しました。ビルドはエラーなくできましたが、実際に画面でテストを行うと、テキストは編集できませんでした。
どこを編集すればよろしいでしょうか。

該当のソースコード

ViewController

1// 2// ViewController.swift 3// plusstack 4// 5// Copyright © 2020 swift-biginners. All rights reserved. 6// 7 8import UIKit 9 10class ViewController: UIViewController { 11 12 @IBOutlet weak var stackView : UIStackView! 13 14 var count:Int = 0 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 // Do any additional setup after loading the view. 19 } 20 21 @IBAction func tapAddViewButton(_ sender:Any){ 22 let newView = CustomView() 23 newView.backgroundColor = UIColor.green 24 newView.layer.borderColor = UIColor.black.cgColor 25 newView.layer.borderWidth = 1.0 26 27 28 count += 1 29 newView.heightAnchor.constraint(equalToConstant: 100.0).isActive = true 30 newView.translatesAutoresizingMaskIntoConstraints = false 31 stackView.addArrangedSubview(newView) 32 } 33 34} 35 36

CustomView

1// 2// CustomView.swift 3// plusstack 4// 5// Copyright © 2020 swift-biginners. All rights reserved. 6// 7 8import UIKit 9 10class CustomView: UIView,UITextViewDelegate { 11 /* 12 // Only override draw() if you perform custom drawing. 13 // An empty implementation adversely affects performance during animation. 14 override func draw(_ rect: CGRect) { 15 // Drawing code 16 } 17 */ 18 let deleteButton = UIButton() 19 let Rolenamelabel = UILabel() 20 let ProtTextView = UITextView() 21 22 init(){ 23 super.init(frame: CGRect()) 24 25 26 27 28 deleteButton.setTitle("x", for: .normal) 29 deleteButton.setTitleColor(UIColor.systemBlue, for: .normal) 30 deleteButton.sizeToFit() 31 32 deleteButton.addTarget(self, action: #selector(tapDeleteButton(_:)), for: .touchUpInside) 33 34 addSubview(deleteButton) 35 deleteButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true 36 deleteButton.topAnchor.constraint(equalTo: topAnchor, constant:2).isActive = true 37 deleteButton.translatesAutoresizingMaskIntoConstraints = false 38 39 40 41 Rolenamelabel.text = "テスト" 42 43 Rolenamelabel.textColor = UIColor.black 44 Rolenamelabel.font = UIFont.systemFont(ofSize: 15.0) 45 Rolenamelabel.textAlignment = NSTextAlignment.center 46 Rolenamelabel.layer.borderColor = UIColor.black.cgColor 47 Rolenamelabel.layer.borderWidth = 2.5 48 Rolenamelabel.layer.cornerRadius = 6.0 // 角の半径 49 50 51 addSubview(Rolenamelabel) 52 53 Rolenamelabel.translatesAutoresizingMaskIntoConstraints = false 54 Rolenamelabel.topAnchor.constraint(equalTo:self.topAnchor).isActive = true 55 Rolenamelabel.leftAnchor.constraint(equalTo: deleteButton.rightAnchor).isActive = true 56 Rolenamelabel.heightAnchor.constraint(equalToConstant: 30).isActive = true 57 Rolenamelabel.widthAnchor.constraint(equalToConstant: 60).isActive = true 58 59 60 ProtTextView.delegate = self 61 ProtTextView.text = "ここはセリフの部分です。" 62 addSubview(ProtTextView) 63 ProtTextView.translatesAutoresizingMaskIntoConstraints = false 64 ProtTextView.topAnchor.constraint(equalTo:self.topAnchor).isActive = true 65 ProtTextView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true 66 ProtTextView.leftAnchor.constraint(equalTo: Rolenamelabel.rightAnchor).isActive = true 67 ProtTextView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true 68 ProtTextView.isUserInteractionEnabled = true 69 ProtTextView.isSelectable = true 70 ProtTextView.isEditable = true 71 ProtTextView.autocorrectionType = UITextAutocorrectionType.yes 72 ProtTextView.spellCheckingType = UITextSpellCheckingType.yes 73 let myGesture = UITapGestureRecognizer(target: self, action: #selector(CustomView.tappedAwayFunction(_ :))) 74 self.ProtTextView.addGestureRecognizer(myGesture) 75 76 77 } 78 79 required init?(coder aDecoder: NSCoder) { 80 fatalError("init(coder:) has not been implemented") 81 } 82 83 @objc func tapDeleteButton(_ sender:UIButton){ 84 removeFromSuperview() 85 } 86 87 @objc func tappedAwayFunction(_ sender: UITapGestureRecognizer){ 88 ProtTextView.resignFirstResponder() 89 } 90 91} 92

補足情報

そもそもの話としてですが、UITapGestureRecognizerはViewController.swiftとCustomView.swiftのどちらで提議した方がよろしいでしょうか。質問が複数になりますが、よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

textViewをタップしてキーボード表示からの編集にtapGestureRecognizerは必要ありません。

swift

1//let myGesture = UITapGestureRecognizer(target: self, action: #selector(CustomView.tappedAwayFunction(_ :))) 2//self.ProtTextView.addGestureRecognizer(myGesture)

キーボードを表示させるだけでしたら、↑の2行をコメントアウトしてください。simulatorのキーボードを表示する設定がされていればキーボードが出現して編集可能となります。

追加する子viewが多くなる?ならUITableViewの使用を強くおすすめします。

投稿2020/05/23 13:32

編集2020/05/23 14:20
退会済みユーザー

退会済みユーザー

総合スコア0

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

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

off927

2020/05/23 15:10

返信が遅くなってしまい、申し訳ございません。今、コメントアウトのほうを実行しましたら、無事、編集ができました。ありがとうございます! ちなみになのですが、なぜUITableViewを強くお勧めされるのか、そのメリットをご教授いただけたら幸いです。
退会済みユーザー

退会済みユーザー

2020/05/23 15:18

導入は面倒くさいかもしれませんが、 同じようなものが繰り返しリスト状に表示されるデータ/表示の管理を助けてくれるので。 というか画面の表示領域は限られるiOSアプリの基本だからかな?
off927

2020/05/23 15:26

ありがとうございます!ぜひ参考にさせていただきます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問