【问题标题】:ResignFirstResponder not workingResignFirstResponder 不工作
【发布时间】:2012-09-28 12:00:25
【问题描述】:
-(IBAction)settings:(id)sender
{
    [mileagerate resignFirstResponder];

    [mainView bringSubviewToFront:mileageView];
    mainView.hidden=TRUE;

    [UIView beginAnimations:@"Settings" context:nil];
    [UIView setAnimationDuration:1.0];
    [mainView setFrame:CGRectMake(0, 0, 320, 460)];
    [UIView commitAnimations];
    mileagerate.text = [[NSUserDefaults standardUserDefaults] valueForKey:@"savedstring"];

    mileageView.hidden=FALSE;
}

我在我的应用程序的各个地方都使用了 resign first responder。但是当我单独点击这个按钮时,键盘并没有消失。如果我点击键盘上的回车键,输入的值就会消失。

【问题讨论】:

  • 你想做什么??????在按钮上单击退出 textView 或什么?
  • 您的 IBAction 是否正常工作?例如。放置一个NSLog 以确保它连接正确

标签: iphone objective-c resignfirstresponder


【解决方案1】:

如果您不知道要辞职哪个textfield,请在您的方法中使用以下代码行:

斯威夫特

self.view.endEditing(true)//resign current keyboard 

目标-C

[self.view endEditing:YES]; //resign current keyboard 

编辑注意:它将从您当前的view 变为resign keybord

【讨论】:

  • 它在模拟器中工作,但在设备中不工作。你知道是什么问题吗?
  • 在这个问题中,您将不得不调试代码并检查流程,因为在关闭当前文本文件后,另一个文本字段可能成为第一响应者。
  • 那不是问题...实际上我之前没有辞职成为FirstResponder,所以我面临这个问题..所以谢谢
  • 在我的情况下这不起作用,因为我在特定情况下覆盖了textFieldShouldEndEditing 并返回false。一旦我将其更改为返回true,键盘终于按预期消失了。
【解决方案2】:

如果您已将 TextFieldDelegate 放在那里,请检查您的 .h 文件。 .有时如果我们不声明它的委托函数就不能正常工作。

【讨论】:

    【解决方案3】:

    我认为当您设置文本时,textField mileagerate 再次成为第一响应者,因此在方法的最后使用此语句

    [mileagerate resignFirstResponder];
    

    【讨论】:

      【解决方案4】:

      您必须首先使用 [UIView setAnimationDidStopSelector:@selector(yourMethod)];

      然后在“YourMethod”中退出你的键盘。

      - (void)yourMethod
      {
         [mileagerate resignFirstResponder];
      }
      

      【讨论】:

        【解决方案5】:

        在 Swift 2 中,您可以使用以下 3 种不同的类型来关闭键盘(假设有一个文本字段和一个按钮

            import UIKit
        
            class ViewController: UIViewController, UITextFieldDelegate {
        
                @IBOutlet var userInputField: UITextField!
        
                override func viewDidLoad() {
                    super.viewDidLoad()
                    //Important if you are using textfieldDelegate
                    self.userInputField.delegate = self
                }
        
        
                @IBAction func findAgeBtn(sender: UIButton) {
        
        //1. keyboard to disappear when button clicked
                    dimissKeyboard()
        
                }
        
        //2. keyboard to disappear when touched anywhere on the view
        
            override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
                self.view.endEditing(true)
        
            }
        
        //Function for option 1
                func dimissKeyboard(){
                    self.view.endEditing(true)
        }
        
        
        //3. keyboard to disappear when clicked on the Return key. Don't forget to set the delegate to self
            func textFieldShouldReturn(textField: UITextField) -> Bool {
                            textField.resignFirstResponder()
                        return true
                    }
                }
        

        【讨论】:

          【解决方案6】:

          确保您已连接返回键以退出第一响应者

          在连接选项卡上将连接更改为“退出时结束”并连接到视图控制器(橙色球)将键盘返回键更改为完成。

          【讨论】:

            【解决方案7】:

            试试这个:

            if([mileagerate isFirstResponder])
                    [mileagerate resignFirstResponder];
            

            【讨论】:

              【解决方案8】:

              1.检查 UITextFieldDelegate ,

              class YourClass : UIViewController,UITextFieldDelegate{
              

              2.然后重写“应该结束编辑”函数使其返回是

                  func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {  
                  return true
              }
              

              【讨论】:

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