1

i want to use a segmented control instead of a UITabBar controller to change the view. Is that in accordance with the HIG?

If yes, how can I do so? What template should I use for my project and what code?

Dave DeLong
  • 239,073
  • 58
  • 443
  • 495
Moshe
  • 55,729
  • 73
  • 263
  • 420

2 Answers2

6

There are several Apple apps that use a segmented control to change the view: iTunes, the App Store app and the YouTube app. However, they all use it at the top of the screen, and in conjunction with a segmented control. The Maps app uses one at the bottom of the screen, but the idea is different from a tab bar. The Calendar app also uses one, for List, Day and Month views (in the toolbar at the bottom).

I think the rule of thumb is that if you're providing different views of the same kind of data, you can use a segmented control. If the things that you're displaying are unrelated, you should use a tab bar. You probably wouldn't have an app rejected for using a segmented control, but users would be a bit confused if you used it in a non-standard way.

I'm not sure about sample code, the closest one I could see is the "Top Songs" sample. That doesn't swap views, it just changes the FetchedResultsController when the segment is clicked on.

Here's another question on SO that's pretty much the same:

How do I use a UISegmentedControl to switch views?

Community
  • 1
  • 1
nevan king
  • 108,735
  • 42
  • 196
  • 237
1

This is how I did it:

-(IBAction)segmentedControlIndexChanged{
switch (self.segmentedControl1.selectedSegmentIndex) {
    case 0:
        [details1 removeFromSuperview];
        [details2 removeFromSuperview];
        [details3 removeFromSuperview];
        [details addSubview:details0];
        break;
    case 1:
        [details2 removeFromSuperview];
        [details0 removeFromSuperview];
        [details3 removeFromSuperview];
        [details addSubview:details1];
        break;
    case 2:
        [details0 removeFromSuperview];
        [details1 removeFromSuperview];
        [details3 removeFromSuperview];
        [details addSubview:details2];
        break;
    case 3:
        [details0 removeFromSuperview];
        [details1 removeFromSuperview];
        [details2 removeFromSuperview];
        [details addSubview:details3];
        break;
    default:
        break;
}
}

Make sure you bind the valueChanged method of the segmented control in IB to this IBAction.

Erik
  • 1,998
  • 17
  • 16
  • Good solution, but this doesn't really scale well — is there a more elegant way? – fatuhoku Mar 12 '14 at 10:10
  • You could simply create an association between the switching views in an array and the selected index of the segmented control. Then using the segmented view methods you could control the switching of the views. – dmcqu314 May 20 '15 at 05:04