【问题标题】:resign first responder UITextfield辞职第一响应者 UITextfield
【发布时间】:2016-05-26 11:44:39
【问题描述】:

很抱歉发布此内容,因为它已被问过一百万次,但每当我尝试使用其中一种解决方案时,它要么已过时,要么无法与我的代码一起使用。 (或者也许我做错了)

我有 5 个文本字段和一个文本视图,用于发送电子邮件,我正在尝试在完成后最小化键盘,但我不知道如何操作。

我也尝试让它转到下一个文本字段,直到最后一个,但这不需要这样做。

func textView(textView: UITextView, shouldChangeTextInRange: NSRange, replacementText text: String) -> Bool {
    if text == "\n" {
        text6.resignFirstResponder()
        return false
    }
    return true
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if textField == self.text1 {
        self.text2.becomeFirstResponder()
    }
    else if textField == self.text2 {
        self.text3.becomeFirstResponder()
    }
    else if textField == self.text3 {
        self.text4.becomeFirstResponder()
    }
    else if textField == self.text4 {
        self.text5.becomeFirstResponder()
        self.text5.resignFirstResponder();
    }
    
    return true;
}

【问题讨论】:

  • 检查每个字段是否有一些非零长度的内容并在他们这样做时辞职不是更好吗?你也可以在键盘顶部放一个完成按钮...
  • 你设置的委托方法? stackoverflow.com/questions/11553396/…

标签: ios swift resignfirstresponder


【解决方案1】:

首先你必须在 .h 中添加 UITextFieldDelegate

然后在.m中

yourtextfield.delegate = self

然后应用此代码

func textFieldShouldReturn(textField: UITextField!) -> Bool {
    textField.resignFirstResponder()
    return true;
}

【讨论】:

    【解决方案2】:

    @彼得博森:

    完成后最小化键盘。即当按下“return”键时......在代码的主要部分制作 cmets 之前,您需要 x2 东西......

    1. 在您拥有class ViewController: UIViewController 的视图控制器(代码)的顶部 - 添加UITextFieldDelegate。所以最后应该是这样的:

      class ViewController: UIViewController, UITextFieldDelegate

    2. 在同一个View Controller(代码)的funcviewDidLoad中添加 self.textField.delegate = self

    然后...如“Abhinandan Pratap”所述,合并以下代码以控制按下“Return”时键盘的行为:

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    } 
    

    但是,如果您想通过点击屏幕上的其他位置来最小化键盘,则使用“TouchesBegan”就足够了(前提是您没有使用滚动视图)

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }
    

    如果您在屏幕上使用scrollView,您需要使用self.view.endEditing(true)UITapGesture 来使点击外部关闭键盘。

    【讨论】:

    • 非常感谢,这非常有效!非常感谢您如此详细地解释它,它创造了奇迹! :D 我不知道该给谁正确答案,但由于是“Abhinandan Pratap”首先编写了代码,所以我把它给了他,但是谢谢你! :)
    • touchesBegan 的声明变了,所以应该是这样的:func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?)
    【解决方案3】:

    这样就可以了

    self.view.endEditing(true)
    

    【讨论】:

    • 我应该在哪里粘贴这个? :)
    • 当你需要隐藏键盘时使用它而不是 resignFirstResponder
    【解决方案4】:

    有时使用两种方式都会有所帮助(Swift 4):

    首先使用声明一个委托UITextFieldDelegate,其中您有textFieldShouldReturn 方法。

    其次你必须在初始化中设置self.textField.delegate = self 部分(像往常一样在viewWillAppear(_ animated: Bool)viewDidLoad)。

    然后填充方法为:

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.resignFirstResponder()
        self.view.endEditing(true)
        //or use on superView of textField
        //superView.endEditing(true)
        return true
    }
    

    【讨论】:

      【解决方案5】:

      首先你必须确认UITextFieldDelegate

       func textFieldShouldReturn(textField: UITextField) -> Bool
       {
           textField.resignFirstResponder()
           return true
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        相关资源
        最近更新 更多