swift - 尝试在快速单击文本字段之外的任何位置时实现隐藏键盘
问题描述
我可以输入密码文本字段并单击外部任何位置并隐藏键盘,但是每当我输入电子邮件文本字段并单击文本字段或登录按钮之外的任何位置时,我都会收到错误消息。
这是我的代码:
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)
}
}
解决方案
你可以试试下面的,从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)
}
推荐阅读
- php - mouseover 显示原始 html 数据并触发无限时间
- sql - 在我的情况下,如何使用 SQL Server 将行转换为列?
- javascript - 如何根据事件以不同颜色突出显示日历中的日期?
- swift - 如何编码/解码 [CKRecordZone.ID: CKServerChangeToken]?
- scala - 带有过滤器列的 Scala
- php - Laravel @extends 和 @include 有什么区别
- python - 如何在一个字符串中找到字母,数字然后在python中放入换行符
- python - 使用scrapy提取带有标题和带有选定链接的url的链接
- html - 需要设计显示带有设置图标的 HTML (Div)
- mysql - 如何在一行中显示值字段但分开