如果爲segmentBar上的每個段使用不同的viewController,則必須使用容器viewController,將每個viewController的視圖添加爲自己的子視圖或將其視圖設置爲viewController的視圖。例如:
UIViewController* containerController = [[[UIViewController alloc] init] autorelease];
//Inside the viewDidLoad of the the ContainerController class, do the following:
//Initialize all three viewControllers
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
UIViewController* test1 = [[[UIViewController alloc] init] autorelease];
//set up the segment and add it to the container's navBar's title view.
[segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
- (void)segmentValueChanged:(id)sender
{
//if first tab selected
[self.view removeAllSubviews];
[self.view addSubview:test1.view];
//if second tab selected
[self.view removeAllSubviews];
[self.view addSubview:test2.view];
//if third tab selected
[self.view removeAllSubviews];
[self.view addSubview:test3.view];
}
而是將其添加爲一個子視圖,你也許可以只設置self.view = test1.view
。顯然,你可以使用容器視圖來初始化navController,並將該navController放入彈出窗口中。希望這可以幫助!
感謝您的答案,@Bittu,這實際上是我最終做的。只是想看看是否有更優雅的方式來做到這一點。可能不會。再次感謝! – baselq