user594161
user594161

Reputation:

Need Help Setting Up UISegmentedControl

I have a view that has a UISegmentedControl in its bottom UIToolBar with 2 segments. The view when loaded should default to view 1. Then when segment 2 is selected, it should switch to view 2, etc.

Right now, when I click segment 2, it hides the view 1, then switches to the 2nd view, but how do I keep the segmentedControl displayed? When view 1 is hidden, the control is hidden as well.

Do I need to create 3 views total? And have views 1 and 2 as subviews of the default view that only has the segmented control in it?

EDIT:

- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
    LogResultsViewController* v1 = [[LogResultsViewController alloc] initWithNibName: @"LogResultsViewController" bundle:nil];
    CalendarController* v2 = [[CalendarController alloc] initWithNibName: @"CalendarController" bundle:nil];

    if (index == 0)
    {
        [self.view addSubview: v1.view];      
    }
    else
    {
        [self.view addSubview: v2.view];
    }
}

And this is the code used to load this view:

- (void)loadView 
{
    UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(dismissCalendarView)];
    self.navigationItem.leftBarButtonItem = actionButton;
    [actionButton release];

    int statusBarHeight = 20;
    CGRect applicationFrame = (CGRect)[[UIScreen mainScreen] applicationFrame];
    self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight, applicationFrame.size.width, applicationFrame.size.height)] autorelease];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.view.backgroundColor = [UIColor grayColor];
    calendar.frame = CGRectMake(0, 0, calendar.frame.size.width, calendar.frame.size.height);

    [self.view addSubview:calendar];
    [calendar reload];
}

Upvotes: 0

Views: 450

Answers (2)

Moshe
Moshe

Reputation: 58097

Yes, you seem to have solved your own problem. Your two swappable views need to be inside of a third view. If not, your switching control will be hidden when the parent view is hidden.

Upvotes: 1

Matt Wilding
Matt Wilding

Reputation: 20163

Yes, that's exactly what you need to do. Set up a container view that holds the toolbar and the other views. Then add and remove the other two views as subviews of the container view as needed.

Upvotes: 0

Related Questions