2017-09-14 7 views

答えて

0

私はこれをテストしました。

class ViewController: UIViewController, UITextViewDelegate { 
    @IBOutlet weak var myTextView: UITextView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     myTextView.delegate = self 
    } 

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool 
    { 
     if(text == "\n") 
     { 
      view.endEditing(true) 
      return false 
     } 
     else 
     { 
      return true 
     } 
    } 
} 
+0

ありがとうございました。 –

+0

あなたが指定した場合は、通話の代理人がその通話を受けることになりますが、それは重要ではありません。完了、実行、次に、Interface Builderで選択したものであれ、プログラムで設定したものであれ。 –

+0

@ChrisG。デリゲートを設定しましたか? –

1

そのtextViewDidChangeで改行"\n"を探すことにより行うことが可能です。

そうでない場合は、良いアイデアはreplacementText == "\n"

例をチェックしてfunc textView(UITextView, shouldChangeTextIn: NSRange, replacementText: String)を使用することです:

func textView(UITextView, shouldChangeTextIn: NSRange, replacementText: String){ 
    if replacementText == "\n"{ 
     // do your stuff here 
     // return false here, if you want to disable user from adding newline 
    } 
    // otherwise, it will return true and the text will be replaced 
    return true 
} 
+0

おかげで、それが入力し、[OK]ボタンのbouthになりますあなたの要件を満たすように思われます。新しい行を追加できるようにする必要があります。またそれは "\ n"です:-) –

+0

どのokボタン? 「完了」ボタンを意味しますか? AFAIKは、UITextViewで "\ n"を生成します。 – ramacode

+0

ありがとうございます - Returnキー - 更新された質問をご覧ください。 –

0

使用textFieldShouldReturn:方法

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    if textField == yourTextField { 
     //do something 
    } 

    return true 
} 
関連する問題