How do I use a UISegmentedControl to switch views?

The simplest approach is to have two views that you can toggle their visibility to indicate which view has been selected. Here is some sample code on how it can be done, definitely not an optimized way to handle the views but just to demonstrate how you can use the UISegmentControl to toggle the visible view.

Up vote 16 down vote favorite 11 share g+ share fb share tw.

I'm trying to figure out how to use the different states of a UISegmentedControl to switch views, similar to how Apple does it in the App Store when switiching between 'Top Paid' and 'Top Free'. Iphone cocoa-touch uiview uisegmentedcontrol link|improve this question edited Jan 21 '10 at 13:19Ronnie Liew4,42682532 asked Jun 26 '09 at 2:49Mark Adams8,3782924 100% accept rate.

The simplest approach is to have two views that you can toggle their visibility to indicate which view has been selected. Here is some sample code on how it can be done, definitely not an optimized way to handle the views but just to demonstrate how you can use the UISegmentControl to toggle the visible view: - (IBAction)segmentSwitch:(id)sender { UISegmentedControl *segmentedControl = (UISegmentedControl *) sender; NSInteger selectedSegment = segmentedControl. SelectedSegmentIndex; if (selectedSegment == 0) { //toggle the correct view to be visible firstView setHidden:NO; secondView setHidden:YES; } else{ //toggle the correct view to be visible firstView setHidden:YES; secondView setHidden:NO; } } You can of course further re-factor the code to hide/show the right view.

In my case my views are quite complex and I cannot just change the hidden property of different views because it would take up too much memory. I've tried several solutions and non of them worked for me, or performed erratically, specially with the titleView of the navBar not always showing the segmentedControl when pushing/popping views. I found this blog post about the issue that explains how to do it in the proper way.

Seems he had the aid of Apple engineers at WWDC'2010 to come up with this solution. redartisan.com/2010/6/27/uisegmented-con... The solution in this link is hands down the best solution I've found about the issue so far. With a little bit of adjustment it also worked fine with a tabBar at the bottom.

Thank you for the great find. Definitely a nice and elegant solution for this methodology. – Shiun Nov 10 '10 at 19:49 1 I tried to get this working properly with the toolbar at the bottom without success, stackoverflow.com/questions/4748120/… Can you please help me out?

– Erik Jan 20 '11 at 15:19 Is there a way to have an horizontal flip animation in between Views. Or it only works without animation? – Patrick May 12 '11 at 9:54 Yes, this seems to be a great solution, but how do I adjust this to work with tabBarController with navigationControllers already inside?

– Vova Stajilov Mar 26 at 12:37.

Or if its a table, you can reload the table and in cellForRowAtIndex, populate the table from different data sources based on the segment option selected.

One idea is to have the view with the segmented controls have a container view that you fill with the different subviews (add as a sole subview of the container view when the segments are toggled). You can even have separate view controllers for those subviews, though you have to forward on important methods like "viewWillAppear" and "viewWillDisappear" if you need them (and they will have to be told what navigation controller they are under). Generally that works pretty well because you can lay out the main view with container in IB, and the subviews will fill whatever space the container lets them have (make sure your autoresize masks are set up properly).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions