如何将文本字段的值传递给Swift中的按钮单击函数?

时间:2015-12-29 00:33:03

标签: ios swift scope swift2

我需要在按下登录按钮时执行用户名和登录检查。我需要在没有故事板的情况下完成所有这些工作。但是,我的问题是,当我创建一个连接到函数的按钮时,文本字段就超出了范围。

覆盖func viewDidLoad(){             super.viewDidLoad()

        let Login = UIButton()
        makeButton(Login, name: "Login", location: CGRectMake((screenSize.width-120)/2, 5*screenSize.width/6, 120, 30))
        Login.addTarget(self, action: "loginChecks", forControlEvents: .TouchUpInside)


        // Makes Text Fields
        let usernameField: UITextField = UITextField()
        makeTextField(usernameField, frame: CGRectMake((screenSize.width-300)/2 + 300, 175, 300, 35))

        let passwordField: UITextField = UITextField()
        makeTextField(passwordField, frame: CGRectMake((screenSize.width-300)/2 + 300, 350, 300, 35))

        // Makes Text Labels
        let usernameLabel: UILabel = UILabel()
        makeLabel(usernameLabel, name: "Username", frame: CGRectMake((screenSize.width-300)/2 - 300, 175, 300, 55))

        let passwordLabel: UILabel = UILabel()
        makeLabel(passwordLabel, name: "Password", frame: CGRectMake((screenSize.width-300)/2 - 300, 350, 300, 55))

        self.configureView()

    }

**这是ViewDidLoad **

之外的单独功能
func loginChecks(){
            // Checks login logic

inputsPassword和enteredUsername似乎都在范围之外

            var inputtedPassword = passwordField.text
            var inputtedUsername = usernameField.text
            print(inputtedUsername)
            print(inputtedPassword)

            if (inputtedUsername == ""){
                makeAlert("No Username", message: "Please input a username.", printStatement: "No username")
                return
            } else if(inputtedPassword == ""){
                makeAlert("No Password", message: "Please input a password.", printStatement: "No password")
                return
            } else {

            }

        }

1 个答案:

答案 0 :(得分:2)

您应该在类中声明usernameFieldpasswordField作为属性,这样您就可以从多种方法访问它们。 E.g:

class ViewController: UIViewController {

let usernameField: UITextField = UITextField()
let passwordField: UITextField = UITextField()

override func viewDidLoad() {
    super.viewDidLoad()

    let Login = UIButton()
    makeButton(Login, name: "Login", location: CGRectMake((screenSize.width-120)/2, 5*screenSize.width/6, 120, 30))
    Login.addTarget(self, action: "loginChecks", forControlEvents: .TouchUpInside)


    // Makes Text Fields
    makeTextField(usernameField, frame: CGRectMake((screenSize.width-300)/2 + 300, 175, 300, 35))

    makeTextField(passwordField, frame: CGRectMake((screenSize.width-300)/2 + 300, 350, 300, 35))

    // Makes Text Labels
    let usernameLabel: UILabel = UILabel()
    makeLabel(usernameLabel, name: "Username", frame: CGRectMake((screenSize.width-300)/2 - 300, 175, 300, 55))

    let passwordLabel: UILabel = UILabel()
    makeLabel(passwordLabel, name: "Password", frame: CGRectMake((screenSize.width-300)/2 - 300, 350, 300, 55))

    self.configureView()
}

func loginChecks(){
    // Checks login logic
    var inputtedPassword = passwordField.text
    var inputtedUsername = usernameField.text
    print(inputtedUsername)
    print(inputtedPassword)

    if (inputtedUsername == ""){
        makeAlert("No Username", message: "Please input a username.", printStatement: "No username")
        return
    } else if(inputtedPassword == ""){
        makeAlert("No Password", message: "Please input a password.", printStatement: "No password")
        return
    } else {

    }
}

}