Reputation: 483
I need to preform username and login checks upon pressing the login button. I need to do all of this without storyboards. However, my problem is that when I create a button that connects to a function, the textfields are then out of scope.
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
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()
}
** This is a separate function outside ViewDidLoad **
func loginChecks(){
// Checks login logic
both inputtedPassword and inputtedUsername appear to be outside of scope
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 {
}
}
Upvotes: 3
Views: 3944
Reputation: 389
You should declare usernameField
and passwordField
as properties in your class, that way you can access them from multiple methods.
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 {
}
}
}
Upvotes: 2