I am coding in xCode 4.3
This is my first application.
I have a UIViewController, company logo on top, then search bar and then UITableView in the middle (with product names) and bottom footer image. Now I want that once an item is clicked on UITableView, only TableView is replace with a view showing product details.
Right now I can replace the entire view with following code:
if (!self.prodDetailViewController_)
{
self.prodDetailViewController_ = [[PCS1ProdDetailViewController alloc] initWithNibName:@"PCS1ProdDetailViewController" bundle:[NSBundle mainBundle]];
}
[self presentModalViewController:prodDetailViewController_ animated:YES];
But it just increases my work, because I will have to redo the top bar and bottom bar (which remains same in entire application) in all my views.
Is there a way that I just change the element of my main UIViewController to UITableView.
Thanking you in anticipation.
I’m going to assume you’re able to use iOS 5 features here. What you can do is implement a container view controller – much like UINavigationController, but with your own view layout so you can keep logo, search bar, etc. all in place and only transition between views in a part of your view.
I created a new container view controller named
ViewController. It has a UIView outletcontainerViewwhich is set up in the .xib file, along with a top bar, search bar, and bottom bar (corresponding to the other views you describe in your application). It also has propertiestableViewControlleranddetailViewController. In itsviewDidLoadimplementation, it adds aTableViewControllerinstance as a child view controller. When the table view is tapped, the view controller adds aDetailViewControllerinstance as a child view controller and transitions to it. Tapping a button on the detail view transitions back to the table view, and removes the detail view controller as a child.Here’s my
viewDidLoadmethod:I have a delegate method so that the container knows when a table row is tapped, and does the transition between the table view and the detail view. Its implementation follows:
As mentioned above, the detail view executes a block when tapping on a back button. I create this block in
ViewControllerhere:That’s about all there is to it. Be sure to read the “Implementing a Container View Controller” section of the UIViewController class reference for more details. Hope this helps!