2011-03-07 81 views
0

我有一個標籤欄應用程序。在其中一個選項卡中,我想在頂部導航視圖中使用uisegmentedControl,它控制當前顯示的視圖。如果我只是交換視圖,這很容易,但我想以更有組織和通用的方式進行操作,對每個視圖使用一個uiviewcontroller並以最優化的方式交換它們。實現我自己的導航控制器?

我想第一步就是要知道tabbar控制器在更改選項卡時發送給導航控制器/視圖控制器的確切內容,然後從中解決問題。

任何一個人都可以指向正確的方向嗎?

回答

3

前段時間我偶然發現了SegmentsController我在從紅色工匠這個blog條目中找到實施。
我與UITabBarController一起使用它,但不知道我做錯了。沒有錯,因爲它「崩潰」或「它沒有做我想做的事」,但錯誤的是我必須將每個UIViewController調用(如viewDidAppear,receivedMemoryWarning等)轉發給子viewControllers。帶有錯誤代碼的應用程序仍在應用程序商店中,我從未收到過抱怨。

但我玩了一段時間,並想出如何正確使用它。這有點麻煩,但恕我直言,這絕對是值得的。 我會告訴你我現在有正確的版本,我在Interface Builder中創建UITabBarController,所以我必須更改代碼中的選項卡。這引出了另一塊混亂,也許還有改進的餘地。但是現在我對這個解決方案很滿意。

NSMutableArray *items = [self.tabBarController.viewControllers mutableCopy]; // tabs from tabbar configured in IB 

// The two child vc that will appear in the segment control 
SomeViewController_iPhone *tvcs = [[[SomeViewController_iPhone alloc] initWithNibName:@"SomeView_iPhone" bundle:nil] autorelease]; 
SomeOtherViewController_iPhone *tvct = [[[SomeOtherViewController_iPhone alloc] initWithNibName:@"SomeOtherView_iPhone" bundle:nil] autorelease]; 
NSArray *viewControllers1 = [NSArray arrayWithObjects:tvcs, tvct, nil]; 

// the nav controller acts as a wrapper around the child viewcontrollers 
UINavigationController *navController1 = [[[UINavigationController alloc] init] autorelease]; 
navController1.tabBarItem.title = NSLocalizedString(@"FirstTab", nil); 
navController1.tabBarItem.image = [UIImage imageNamed:@"tabImage1.png"]; 
navController1.navigationBar.tintColor = [UIColor navBarTintColor]; 

firstTabSegmentsController = [[SegmentsController alloc] initWithNavigationController:navController1 viewControllers:viewControllers1]; 

// uses a NSArray category that basically creates a NSArray that has the title properties of the vc in viewControllers1 
firstTabSegmentedController = [[UISegmentedControl alloc] initWithItems:[viewControllers1 arrayByPerformingSelector:@selector(title)]]; 
firstTabSegmentedController.frame = CGRectMake(0, 0, 222, 30); 
firstTabSegmentedController.segmentedControlStyle = UISegmentedControlStyleBar; 
firstTabSegmentedController.selectedSegmentIndex = 0; 

[firstTabSegmentsController indexDidChangeForSegmentedControl:firstTabSegmentedController]; 

[firstTabSegmentedController addTarget:firstTabSegmentsController action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged]; 

// replace first tab from interface builder with this 
[items replaceObjectAtIndex:0 withObject:navController1]; 

當你看到它需要一些設置,但在我看來,這個解決方案是比什麼都重要,我在整個時間嘗試更好。我希望我能夠正確地刪除代碼。


編輯:上傳的樣本項目:BeautifulColors.zip

+0

嗨, 非常感謝!我希望今晚有時間來嘗試一下。 你有沒有一個可以發送郵件或上傳的示例項目? 親切的問候 E – Erik 2011-03-08 02:41:26

+0

@Erik只做了一個。請參閱編輯。 – 2011-03-08 09:52:37

+0

完美,我需要的一切! – Erik 2011-03-08 10:32:11

0

只是交換視圖並跟上當前視圖的viewController是在這方面實現UISegmentedControl的最佳方式。

注意:通過交換意見我的意思是添加一個子視圖到當前視圖和刪除舊的。

你可能有興趣在下面的方法,它是由UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;