需要帮助设置UISegmentedControl

时间:2011-06-23 23:27:48

标签: iphone objective-c xcode uiview uisegmentedcontrol

我有一个视图,其底部UIToolBar中有一个带有2个段的UISegmentedControl。加载时的视图应默认为视图1.然后,当选择了段2时,它应切换到视图2等

现在,当我点击第2段时,它会隐藏视图1,然后切换到第2个视图,但是如何保持segmentedControl显示?隐藏视图1时,控件也会被隐藏。

我是否需要共创建3个视图?并将视图1和视图2作为默认视图的子视图,其中只包含分段控件?

编辑:

- (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];
    }
}

这是用于加载此视图的代码:

- (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];
}

2 个答案:

答案 0 :(得分:1)

是的,您似乎已经解决了自己的问题。您的两个可交换视图需要位于第三个视图中。如果没有,隐藏父视图时将隐藏您的切换控件。

答案 1 :(得分:0)

是的,这正是你需要做的。设置容纳工具栏和其他视图的容器视图。然后根据需要添加和删除其他两个视图作为容器视图的子视图。