Robert J. Clegg
Robert J. Clegg

Reputation: 7370

UISearchBar in two view controllers

My app has a UITabBarController which allows the user to switch between screens.

There are two screens.

Tab1: viewController A - which is a subclass of UIViewController. It has a UiSearchBar at the top and other non-related content below.

Tab2: viewController B - is a subclass of UITableViewController and also conforms to the UISearchBarDelegate and UISearchDisplayControllerDelegate.

This view has a UISearchBar and DisplayController at the top.

The is what I want to get working in my app:

When the user starts a search in viewController A - some results will show below the search bar. Then, when the user taps a search result then viewController B will appear on screen and show the results. This will allow me to filter and arrange results as I see fit.

If anyone has seen the latest version of the eBay iOS app - the searching function is what i am trying to get working.

The Problem

I am not worried about how the search works and how filtering works, etc. What I want to know is how do I link the first UISearchBar to display results and take the user to viewController B - with their search terms?

If I look at eBay app - its almost like the bar is the same instance as it keeps the search text.

Upvotes: 0

Views: 514

Answers (2)

Phillip Mills
Phillip Mills

Reputation: 31026

Passing data between controllers works fine for simple programs but I find it gets confusing as the app becomes more complicated. Another approach is to create a real data Model…the 'M' in MVC.

The primary model class that gives access to application state can either be a singleton with a sharedInstance or a unique object that's owned by the app delegate. With that structure, there's no passing of data directly between controllers. Instead, any controller that needs to change state tells the Model to do so and any controller that needs to know state asks the Model for it when displaying or updating views.

Upvotes: 0

Jasper
Jasper

Reputation: 7107

I would: write a protocol XYZ in your UIViewController A

Subclass your UITabBarController and make it conform XYZ

After user presses a search result in UIViewController A, send the delegate (your UITabBarController) a message and let him handle the data (array of your search objects or whatever you need) transfer and switching to UIViewController B

Have a look at this Q on SA about transferring data between UIViewControllers: Passing Data between View Controllers

Upvotes: 0

Related Questions