Reputation: 1
So I'm creating a social media app in Xcode and am trying to create the signup button page. However, I noticed every time I tap on the username, password or any of the other textfields, instead of the ios keyboard popping up I get this error:
unrecognized selector sent to instance
I've looked up how to solve the problem and it says to check your outlets. However, I've checked my outlets a million times and know they are correct. I've even broken and reconnected all of them just to be sure. I know the error pops up when I add the
showKeyboard
and hideKeyboard
functions. I don't know what to do to figure this out. Any ideas what I am doing wrong?
import UIKit
class sign_upVC: UIViewController {
// Profile image
@IBOutlet weak var AvaImg: UIImageView!
// Text fields
@IBOutlet weak var usernameTxt: UITextField!
@IBOutlet weak var passwordTxt: UITextField!
@IBOutlet weak var repeatPassword: UITextField!
@IBOutlet weak var fullnameTxt: UITextField!
@IBOutlet weak var bioTxt: UITextField!
@IBOutlet weak var webTxt: UITextField!
// buttons
@IBOutlet weak var signUpBtn: UIButton!
@IBOutlet weak var cancelBtn: UIButton!
// scrollView
@IBOutlet weak var scrollView: UIScrollView!
// reset default size
var scrollViewHeight : CGFloat = 0
// keyboard frame size
var keyBoard = CGRect()
// default func
override func viewDidLoad() {
super.viewDidLoad()
let scrollView = UIScrollView()
// ScrollView frame sizes
scrollView.frame = CGRect(x: 0,y: 0,width: self.view.frame.width,height: self.view.frame.height)
scrollView.contentSize.height = self.view.frame.height
scrollViewHeight = scrollView.frame.size.height
// Check notification if keyboard is shown or not
NotificationCenter.default.addObserver(self, selector: Selector(("showKeyboard:")), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: Selector(("hideKeyboard:")), name: UIResponder.keyboardWillHideNotification, object: nil)
// declare keyboard if tapped
let hideTap = UITapGestureRecognizer(target: self, action: Selector(("hideKeyboardTap:")))
hideTap.numberOfTapsRequired = 1
self.view.isUserInteractionEnabled = true
self.view.addGestureRecognizer(hideTap)
}
// hide keyboard if tapped
func hideKeyboardTap(recognizer:UITapGestureRecognizer){
self.view.endEditing(true)
}
// show keyboard
func showKeyboard(notification:NSNotification){
// Define keyboard size
keyBoard = ((notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey]! as AnyObject).cgRectValue)!
// move up UI
UIView.animate(withDuration: 0.4, animations: { () -> Void in
self.scrollView.frame.size.height = self.scrollViewHeight - self.keyBoard.height
})
}
// hide keyboard
func hideKeyboard(notification:NSNotification){
// move down UI
UIView.animate(withDuration: 0.4, animations: { () -> Void in
self.scrollView.frame.size.height = self.view.frame.height
})
}
// Tapped sign up
@IBAction func signUpBtn_Tapped(_ sender: Any) {
print("Sign up tapped")
}
// Tapped cancel
@IBAction func cancelBtn_Tapped(_ sender: Any) {
print("Cancel tapped")
self.dismiss(animated: true, completion: nil)
}
}
Upvotes: -1
Views: 675
Reputation: 534885
Never say stuff like
Selector(("showKeyboard:"))
You don't know how to write the Objective C selector for these methods. Use Swift to help you! Write
#selector(showKeyboard)
And
@objc func showKeyboard(...
And so on.
Upvotes: 2