Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

ios - Swift 3 Unrecognized selector sent to instance UIButton

I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITouchesEvent text]: unrecognized selector sent to instance 0x6100000e9400'

The button it crashes on:

import UIKit
import Firebase
import FBSDKLoginKit

class RegisterViewController: UIViewController, FBSDKLoginButtonDelegate, UITextFieldDelegate {

    override func viewDidLoad() {
    super.viewDidLoad()
        // Create Sign Up button
        let signUpButton: UIButton = {
            let button = UIButton()
            button.setImage(#imageLiteral(resourceName: "Go Button@1x"), for: .normal)
            button.addTarget(self, action: #selector(createAccountTapped), for: .touchUpInside) // when tapped on Sign In button, execute function SignInTapped
            button.translatesAutoresizingMaskIntoConstraints = false
            return button
            }()
        func signUpButtonView() {
            signUpButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            signUpButton.heightAnchor.constraint(equalToConstant: 70).isActive = true
            signUpButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true
            signUpButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
        }

        self.view.addSubview(signUpButton)
        signUpButtonView()

    }

    func createAccountTapped(_ usernameTextField: UITextField, passwordTextField: UITextField, repeatPasswordTextField: UITextField) {
        let username = usernameTextField.text
        let password = passwordTextField.text
        let repeatPassword = repeatPasswordTextField.text
    }
}

I have another button for signing up with Facebook and it works fine... I think it's something to do with parameters in the function createAccountTapped but I don't know what's wrong.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can not pass UITextField instance with UIButton action, you can set only these action createAccountTapped() and createAccountTapped(_ sender: UIButton) with UIButton.

If you want to access textField in your UIButton action then you need to create IBOutlet for that textField in your RegisterViewController.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...