Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Rashii Henry
Rashii Henry
16,433 Points

Segmented Control

Any suggestions on how to use a segmented control where each button presents a different view but within the same view controller?

4 Answers

Stone Preston
Stone Preston
42,016 Points

place the control on your view controller

ctrl drag to your header file and create an action for the segmented control. Use the event "Value Changed"

- (IBAction)segmentAction:(id)sender

inside your action implementation

- (IBAction)segmentAction:(id)sender {
 UISegmentedControl *seg = sender;

 if (seg.selectedSegmentIndex == 0) 
     [self performSegueWithIdentifier:@"showMyView" sender:self];

}

you can add checks for however many indexes you have. I guess you could also use a switch statement on the selected index if you dont want to have a lot of if statements

edit: sorry didnt see that you wanted views within a view. You could change the if statements to show and hide the corresponding views ie

if (seg.selectedSegmentIndex == 0) {

self.view1.hidden = NO;
self.view2.hidden = YES;
self.view3.hidden = YES;
self.view4.hidden = YES;



}
Rashii Henry
Rashii Henry
16,433 Points

okay stone, I like that suggestion you made.

for the first part how can you assign an instance variable to an id type?

next, how do I create my own custom view1 view 2 and etc..?

Is it like UiView *view1 = [[UIView alloc]init];?

Rashii Henry
Rashii Henry
16,433 Points

or do I create a new property?

Stone Preston
Stone Preston
42,016 Points

It sounds like you should probably watch more of the iOS videos again. Have you done the objective C basics course? Those might help with some of your questions which are pretty basic to iOS.

Stone Preston
Stone Preston
42,016 Points

also

  UIView *view1 = [[UIView alloc]init]

would work but then you would also have to add all the stuff you want on that view programatically as well. I would suggest adding the UIViews to an existing View controller in story board. Or simply use separate view controllers for each view (a little less cluttered and confusing)

Rashii Henry
Rashii Henry
16,433 Points

I know how to do it and yes I have seen those videos. I was just trying to get the best approach.

now I have view1 created as an outlet in my header file.

how do go about creating view2, (I know that it should be created in header first) within the same view controller in storyboard?

the same way it looks on Instagram when your viewing your own profile. the different tabs show different views.

Stone Preston
Stone Preston
42,016 Points

then add another view to your view controller and ctrl drag to the header and create the outlet for view 2.