Question

I have a TabBar application, with a "Home" tab and a "Search" tab. In the Home tab, I have a search bar, and when user clicks Search I'd like to copy the text string into the Search tab's view controller and perform the search with that string.

In order to pass the string, I am trying to send a notification and then forcing the TabBarController to switch tabs. Both these steps are working; the issue is, I will only add the observer (in the Search View Controller) after loading it, which will happen only after I load the view for the first time. In other words, the notification is sent before the observer can be properly set up and, consequently, it is ignored.

What is the best solution for this problem?

Edit:

On Home View Controller:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  [[self tabBarController] setSelectedIndex:2]; // Switch to Search VC
  [[NSNotificationCenter defaultCenter] postNotificationName:@"SearchFromHome" object:self userInfo:@{@"Query" : [searchBar text]}];
}

On Search View Controller:

- (void)viewDidLoad {
  [super viewDidLoad];  
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchFromHome:) name:@"SearchFromHome" object:nil];
}

Search View Controller won't observe the notification unless I load it first; method searchFromHome: is not called.

If I try to perform a search right after the app loads (it loads at the Home VC), notification will be ignored since Search VC is not yet set up to observe it.

Was it helpful?

Solution

You can't send the notification to the search view controller as you've rightly surmised that it hasn't been created yet. There are two basic ways I know how to solve this problem:

A) (The bad way) Load the search view controller when the main tab bar controller loads - increases memory load unnecessarily (what if the user never searches?)

B) (The good way) Create a public NSString property on both your search view controller and your home tab controller. Then, set the tab bar controller as an observer of the "SearchFromHome" notification (which it sounds like you might already be doing since you said you are able to get the tab to switch programatically). The tab bar controller should have a method in it that is activated when that notification is posted. In that method check to see if a search view controller has been created, if so, switch to it and set the NSString 'searchText' property to whatever search string was entered in the home tab controller. If it hasn't been created, then create it and set the search string.

Rough Example Code:

//In TabBarController.m
//Be sure to set this class instance as an observer of @"SearchFromHome" notifications 
//with the target being the method below
-(void)searchTabActivator
{
   if (!self.searchBarController) {
      self.searchBarController = //Instantiate view controller code
   }
   self.searchBarController.searchText = self.homeTabController.searchText;
   //Switch to search tab and perform search
}

OTHER TIPS

I'm not sure what you are trying to do but you can create a property for your search bar:

.h file.
 @interface yourcontroller{
  IBOutlet UISearchBar *searchBar;//make sure have set the referencing outlet
 }
// or create the property like this 
 @property (strong, nonatomic) IBOutlet UISearchBar *searchBar;

and in the method search just do this

-(void)search{
   NSString *searchText = searchBar.text;
}

I hope this can help you.

If you want to get notifications you have to observe your sender. If you haven't add observer, you will not receive any notifications. In your case I suggest you to store search string in a property of your search tab controller. And when this controller will appear you may start searching with this string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top