试图在快速单击文本字段以外的任何地方时实现隐藏键盘

问题描述 投票:1回答:2

我能够输入密码文本字段并在外部的任何位置单击并隐藏键盘,但是每当我键入电子邮件文本字段并单击除文本字段或登录按钮之外的任何位置时,我都会收到错误消息。

这是我的代码:

 import UIKit

 class FirstScreen: UIViewController {

     @IBOutlet weak var headerLabel: UILabel!


     @IBOutlet weak var emailLabel: UITextField!
     @IBOutlet weak var passwordLabel: UITextField!



     @IBAction func loginButton(_ sender: UIButton)
     {
         self.passwordLabel.resignFirstResponder()
         self.emailLabel.resignFirstResponder()
         /*
         let emailLabel1 = emailLabel.text!
         let passwordLabel1 = passwordLabel.text!
         if((emailLabel1.contains("email")) && (passwordLabel1.contains("password")))
         {


         }
         else
         {
              print("ERROR")
         }*/

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


 }

这是我得到的错误:

 2020-01-16 17:43:44.645152-0500 Workout Log[3413:176741] -[Workout_Log.FirstScreen 
 emailLabel:]: unrecognized selector sent to instance 0x7fdf95503920
 2020-01-16 17:43:44.670234-0500 Workout Log[3413:176741] *** Terminating app due to 
 uncaught exception 'NSInvalidArgumentException', reason: '-[Workout_Log.FirstScreen 
 emailLabel:]: unrecognized selector sent to instance 0x7fdf95503920'
 *** First throw call stack:
 (
     0   CoreFoundation                      0x00007fff23c7127e __exceptionPreprocess + 350
     1   libobjc.A.dylib                     0x00007fff513fbb20 objc_exception_throw + 48
     2   CoreFoundation                      0x00007fff23c91fd4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   UIKitCore                           0x00007fff480c0f17 -[UIResponder doesNotRecognizeSelector:] + 302
4   CoreFoundation                      0x00007fff23c75c4c ___forwarding___ + 1436
5   CoreFoundation                      0x00007fff23c77f78 _CF_forwarding_prep_0 + 120
6   UIKitCore                           0x00007fff48093fff -[UIApplication sendAction:to:from:forEvent:] + 83
7   UIKitCore                           0x00007fff47a6c00e -[UIControl sendAction:to:forEvent:] + 223
8   UIKitCore                           0x00007fff47a6c358 -[UIControl _sendActionsForEvents:withEvent:] + 398
9   UIKitCore                           0x00007fff48387616 -[UITextField _resignFirstResponder] + 155
10  UIKitCore                           0x00007fff480c01b3 -[UIResponder _finishResignFirstResponder] + 358
11  UIKitCore                           0x00007fff4838725c -[UITextField _finishResignFirstResponder] + 48
12  UIKitCore                           0x00007fff480c0315 -[UIResponder resignFirstResponder] + 275
13  UIKitCore                           0x00007fff483870d6 -[UITextField resignFirstResponder] + 93
14  UIKitCore                           0x00007fff4839f01d -[UIView(UITextField) endEditing:] + 184
15  Workout Log                         0x0000000103cf1c8e $s11Workout_Log11FirstScreenC12touchesBegan_4withyShySo7UITouchCG_So7UIEventCSgtF + 334
16  Workout Log                         0x0000000103cf1d38 $s11Workout_Log11FirstScreenC12touchesBegan_4withyShySo7UITouchCG_So7UIEventCSgtFTo + 136
17  UIKitCore                           0x00007fff480bf863 forwardTouchMethod + 340
18  UIKitCore                           0x00007fff480bf6fe -[UIResponder touchesBegan:withEvent:] + 49
19  UIKitCore                           0x00007fff480ce8de -[UIWindow _sendTouchesForEvent:] + 1867
20  UIKitCore                           0x00007fff480d04c6 -[UIWindow sendEvent:] + 4596
21  UIKitCore                           0x00007fff480ab53b -[UIApplication sendEvent:] + 356
22  UIKitCore                           0x00007fff4812c71a __dispatchPreprocessedEventFromEventQueue + 6847
23  UIKitCore                           0x00007fff4812f1e0 __handleEventQueueInternal + 5980
24  CoreFoundation                      0x00007fff23bd4471 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
25  CoreFoundation                      0x00007fff23bd439c __CFRunLoopDoSource0 + 76
26  CoreFoundation                      0x00007fff23bd3b74 __CFRunLoopDoSources0 + 180
27  CoreFoundation                      0x00007fff23bce87f __CFRunLoopRun + 1263
28  CoreFoundation                      0x00007fff23bce066 CFRunLoopRunSpecific + 438
29  GraphicsServices                    0x00007fff384c0bb0 GSEventRunModal + 65
30  UIKitCore                           0x00007fff48092d4d UIApplicationMain + 1621
31  Workout Log                         0x0000000103cefccb main + 75
32  libdyld.dylib                       0x00007fff5227ec25 start + 1
33  ???                                 0x0000000000000001 0x0 + 1
 )
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

曾尝试修复此问题一段时间,所以任何帮助都将非常有用

编辑:

我尝试实现以下代码,但仍然收到相同的错误:

import UIKit

class FirstScreen: UIViewController {

@IBOutlet weak var headerLabel: UILabel!

@IBOutlet weak var emailLabel: UITextField!

@IBOutlet weak var passwordLabel: UITextField!

func hideKeyboardWhenTappedAround() {

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
    tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)

 }

@objc func dismissKeyboard() {
       self.view.endEditing(true)
 }

override func viewDidLoad() {
    super.viewDidLoad()
    hideKeyboardWhenTappedAround()
    dismissKeyboard()
}

@IBAction func loginButton(_ sender: UIButton)
{
    self.emailLabel.resignFirstResponder()
    self.passwordLabel.resignFirstResponder()
    /*
    let emailLabel1 = emailLabel.text!
    let passwordLabel1 = passwordLabel.text!
    if((emailLabel1.contains("email")) && (passwordLabel1.contains("password")))
    {


    }
    else
    {
        print("ERROR")
    }*/

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




 }
swift mobile keyboard hide
2个回答
1
投票

您可以尝试以下方法,从viewdidload调用它>>

func hideKeyboardWhenTappedAround() {        

    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
    tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)

 }

@objc func dismissKeyboard() {
       self.view.endEditing(true)
 }

-1
投票

该按钮引用的@IBAction方法类型不再存在。通常,这意味着@IBAction方法的名称或类型已更改,并且未更新与该方法的情节提要之间的关系。

© www.soinside.com 2019 - 2024. All rights reserved.