Swift 3 无法识别的选择器发送到实例 UIButton [英] Swift 3 Unrecognized selector sent to instance UIButton

查看:25
本文介绍了Swift 3 无法识别的选择器发送到实例 UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

由于未捕获的异常而终止应用'NSInvalidArgumentException',原因:'-[UITouchesEvent 文本]:无法识别的选择器发送到实例 0x6100000e9400'

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

它崩溃的按钮:

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
    }
}

我有另一个用于注册 Facebook 的按钮,它工作正常...我认为这与函数 createAccountTapped 中的参数有关,但我不知道出了什么问题.

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.

推荐答案

UITextField 实例不能通过 UIButton 动作传递,只能设置这些动作 createAccountTapped()createAccountTapped(_ sender: UIButton)UIButton.

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

如果你想在你的 UIButton 操作中访问 textField 那么你需要为那个 textField 创建 IBOutlet在您的 RegisterViewController 中.

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

这篇关于Swift 3 无法识别的选择器发送到实例 UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆