Я могу ввести текстовое поле пароля и щелкнуть в любом месте снаружи, и клавиатура скрыта, но всякий раз, когда я набираю текстовое поле электронной почты и щелкаю в любом месте, кроме текстовых полей или кнопки входа, я получаю сообщение об ошибке.
Здесь мой код:
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)
Я пытался исправить это какое-то время, чтобы любая помощь была бы замечательной
edit:
Я пытался реализовать следующий код, но все равно получаю ту же ошибку:
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)
}
}