If you’re migrating your skills from web to mobile development it’s really easy to think in terms of passing user defined values from page to page when entering into a mobile environment. UIViewControllers aren’t web pages though, in the UIViewController world the ViewController is an object so no need to pass anything.
Here’s a UISearchBar that I’m using in an App, it’s in a ViewController (HomeScreen) and I want to get the value entered from HomeScreen to another ViewController (SearchResults) so that an API call can be made with the value.
I created a property on the HomeScreen ViewController outside of all the methods to support my needs:
// Holds the search criteria so SearchResultsViewController can access it.
public static string searchCriteria;
this.txtSearch.SearchButtonClicked += (sender, e) =>
{
searchCriteria = string.Empty;
// Populate the Search Criteria Property so it can be accessed from another view controller
searchCriteria = txtSearch.Text;
txtSearch.Text = string.Empty;
if (this._searchResultsViewController == null) {
this._searchResultsViewController = new searchResults ();
}
// Show the SearchResults ViewController
this.NavigationController.PushViewController (this._searchResultsViewController, true);
} ;
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
string retrievedSearchCriteria = string.Empty;
// Access the Home Screen ViewController to get the Search Criteria
retrievedSearchCriteria = HomeScreen.searchCriteria;
// Use the value to search with… do stuff
}
1 comment
Comments are closed.