0

一個PopoverController我想有嵌入在PopoverController一個UISegmentedControl,類似於在該SO問題描述:UISegmentedControl embedded in a UINavigationBar/ItemUISegmentedControl在具有多個視圖控制器

不同的是,我會爲每個不同的視圖控制器查看我想要在彈出窗口中顯示,具體取決於分段控件上的選定索引。我不知道我會如何去做這件事。每當我嘗試在根視圖控制器頂部推送一個新視圖時,UISegmentedControl消失。我只想在兩個viewcontrollers之間切換,同時保持UISegmentedControl可見。這甚至有可能嗎?

在此先感謝!

回答

0

如果爲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放入彈出窗口中。希望這可以幫助!

+0

感謝您的答案,@Bittu,這實際上是我最終做的。只是想看看是否有更優雅的方式來做到這一點。可能不會。再次感謝! – baselq

0

如果您使用presentModalViewController方法在屏幕上顯示您的新視圖控制器,它將始終覆蓋整個屏幕以及它下面的任何內容。這就是它的工作原理。

按照文檔:

在iPhone和iPod touch設備,modalViewController的觀點是 總是呈現全屏。在iPad上,演示文稿取決於modalPresentationStyle屬性中的 值。

做到這一點,仍然能夠控制視圖控制器的位置的方式是創建您自己的演示文稿方法。

+0

首先,感謝您的意見。但恐怕我不明白你的意思。我提出了一個UIPopoverController,所以我使用函數presentPopoverFromRect:inView:permittedArrowDirections:animated:我將UINavigationController(它包含我的自定義視圖控制器)作爲popover的內容VC傳入。糾正我,如果我錯了,但我不認爲presentModalViewController可以在這裏適用 – baselq

相關問題