0

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()

2 Answers2

0

You can easily define a static global value for user GPA value like this:

struct defaultsKeys {
        static var userGPA:Double = 0.0
    }

After that, you can set this value from your textField value in your alert box:

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)")
        defaultsKeys.userGPA = Double(textAlert)!
    }

And you can use this userGPA value in every view controller with calling:

defaultsKeys.userGPA
kkakkurt
  • 2,790
  • 1
  • 30
  • 33
0

I've managed to solve it. Yes, I've been saving it to the DataController but without singleton method. So use the singleton method to save and retrieve data.

i.e.

    static var sharedInstance = DataController {
      private init()
}

Store in a variable:

var value : Double = 0.0

And in TableViewController, call:

Datacontroller.sharedInstance.value

Thanks to kkakkurt for helping me out, but it was really simple. I don't know how I overlooked it.