smriti smriti - 1 month ago 9
Swift Question

Swift save text to an instance/global variable when UIButton is pressed

So I have a UItextfield and a UIButton I was wondering if there is a way to save the text after the button is pressed in an instance variable so that I could use it in my other class.
The thing I am trying to do is to have a user input some text in the textfield on screen 1 and then when the user taps the button I want to take him to screen 2 however in the class of that screen 2 I want to know what text user entered in order to display data accordingly.
Right now I just have an action function for the button and I access text inside it and save it to an instance variable but when I call it in my other class it is empty because I initialized it as empty. So can somebody please help me and tell me how to go about this.
Thanks!

Here is the code for first screen

var searchRecipe = ""
@IBAction func SearchButton(sender: UIButton) {
if let recipe = RecipeSearchBar.text {
searchRecipe = recipe
}
}


Here is the code for second screen. I have connected the button in first screen this screen so when user taps the button he gets here.

var recipeName:[String] = []
var imageURL:[String] = []
var timeInSeconds:[Float] = []
func apiCall()
{
//search recipe API call
var searchRecipe = ""
searchRecipe = RecipesViewController().searchRecipe
print (searchRecipe) //prints nothing
endpoint = "http://api.yummly.com/v1/api/recipes?_app_id=apiId&_app_key=apiKey&q=\(searchRecipe)" //this is where I want to use the searchRecipe text from class 1
}

Answer

Well personally I would do what follows (Based on my interpretation of your issue):

Please note that I take for granted you are implementing a view-controller-based navigation and you know what IBOutlets, IBActions & Segues are

  1. In the first view controller create an IBOutlet property of UITextField named RecipeSearchBar and connect it to the relative text field.
  2. In the second view controller create a variable value of type string;
  3. Change the storyboard segue identifier to "toSecondViewController"
  4. Then in the first view controller create an IBAction to be called when the UIButton is pressed.

In your case:

@IBAction func SearchButton(sender: UIButton) {
    if let recipe = RecipeSearchBar.text {
        searchRecipe = recipe
    }

    //If you are navigating into a new view view controller
    //here you need to call self.presentViewController(...)

}

Inside this action, you are going to call self.presentViewController to display your second view controller, but you have to do one last very important step: pass the value of the UITextField to the instance that will hold the second view controller

To achieve this, override prepareForSegue in your first view controller and share the UITextField value. This method is triggered once you called self.presentViewController for further implementation.

//Did not tested the code but should just be ok
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

       if (segue.identifier == "toSecondViewController")
        {
            //Create the instance
            let secondViewController = segue.destinationViewController
            //Set the value
            secondViewController.value = searchRecipe;
        }
    }

You are now good to go. Hope this helped, if so mark the question for others. Bye