Basically, I wanted the value of the textfield of a UIViewController and then use it to do an if-else statement.
I'll be looping through a dictionary and if value (of the dictionary) is larger than and/or equal to the user input, I'll be displaying the keys by a tableview.
I did an alert box that shows up upon loading the app. I created a function and then called it under viewDidLoad() function. This can be seen here:
func alertBox(){
// create String objects for the title and the message
let title = "Enter your GPA"
let message = "With your help, we can list courses you are most likely eligible for based on the GPA you provide to us."
// create a UIAlertController object
let alert = UIAlertController(title: title,
message: message,
preferredStyle: UIAlertControllerStyle.alert)
// create a UIAlertAction object
//2. Add the text field. You can configure it however you need.
alert.addTextField { (textField) in
textField.text = ""
}
// 3. Grab the value from the text field, and print it when the user clicks OK.
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
if let textAlert = textField?.text {
print("Text field: \(textAlert)")
let item = Double(textAlert)
print(item!)
let unwrappedvalue = item!
self.dc.setUserInput(userI: unwrappedvalue)
print(self.dc.getUserInput())
}
}))
// add the UIAlertAction object to the UIAlertController object
let cancelAction = UIAlertAction(title: "Cancel",
style: .cancel,
handler: nil)
alert.addAction(cancelAction)
// display the UIAlertController object
present(alert, animated: true, completion: nil)
}
And here's my viewDidLoad() function:
override func viewDidLoad() {
super.viewDidLoad()
alertBox()
}
As you can see, I actually did a get/set method in a DataController class. This way I can set the value and then get the value in a later method. I've used it, but it doesn't seem to save my values at all. I've also tried creating a global variable and then later storing the value of the textfield into that variable.
i.e.
var textOfTF : Double = 0.0
And then later in the alertBox() function:
if let textAlert = textField?.text {
print("Text field: \(textAlert)")
let item = Double(textAlert)
print(item!)
self.textOfTF = item!
/*
self.dc.setUserInput(userI: unwrappedvalue)
print(self.dc.getUserInput())
*/
}
Here are my get/set functions in my DataController class:
var userInput : Double = 0.0
func getUserInput() -> Double{
return userInput
}
func setUserInput(userI: Double){
userInput = userI
return
}
It managed to save when I did the alert.addAction method (which is this):
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak alert] (_) in
let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
if let textAlert = textField?.text {
print("Text field: \(textAlert)")
let item = Double(textAlert)
print(item!)
let unwrappedvalue = item!
self.dc.setUserInput(userI: unwrappedvalue)
print(self.dc.getUserInput())
}
}))
But then printing outside that method just printed to me a 0.0 value.
Is there anything wrong in my code? Please advise.
P.S. dc in self.dc.setUserInput(..) is a variable where I declared the DataController class.
In the TableViewController class:
var dc = DataController.sharedInstance
In the DataController class:
static var sharedInstance = DataController()