ios - What is the correct approach to setting and accessing global property across ViewControllers? -
in model have built api class creates function basic api call http://openweathermap.org/api returns closure json output dictionary.
i call function in first viewcontroller returns output string when run println().
the issue having how store output (all basic string types) globally accessible variable can pass through segue custom searchresultviewcontroller view.
my current approach isn't working attempts set empty string @ start of firstviewcontroller, optionally unwrap output string, store in cityname variable , pass through part of segue overriding prepareforsegue function.
i create corresponding property cityname in searchresultviewcontroller.
i have thoroughly checked naming conventions/linking custom viewcontroller classes , want confirm if missing key step in approach.
detailed code below.
firstviewcontroller:
var cityname = "" @ibaction func searchbutton() { let api = api() api.weathersearch(urlsearch: searchfield.text!) { dictionary in println(dictionary) if var cityname = dictionary["name"] as? string { println(cityname) } } self.performseguewithidentifier("search", sender: nil) } override func prepareforsegue(segue: uistoryboardsegue!, sender: anyobject!) { if(segue.identifier == "search") { var searchresult = segue!.destinationviewcontroller searchresultviewcontroller; searchresult.cityname = cityname } }
searchresultviewcontroller:
class searchresultviewcontroller: uiviewcontroller { var cityname: string! @iboutlet weak var picture: uilabel! override func viewdidload() { super.viewdidload() println(cityname) } }
change searchbutton()
@ibaction func searchbutton() { let api = api() api.weathersearch(urlsearch: searchfield.text!) { dictionary in println(dictionary) if var cityname = dictionary["name"] as? string { println(cityname) self.performseguewithidentifier("search", sender: nil) } } }
if works reason approach doesn't work network request asynchronous
Comments
Post a Comment