2010-07-24 27 views
0

我的選項卡欄有10個選項卡。其中六個因此被推入「更多」標籤。其中有幾個沒有UINavigationControllers。即,該選項卡由不是導航控制器的UIViewController子類控制。iPhone - 在用戶選擇moreNavigationController中的選項卡時添加一個導航欄按鈕

當用戶選擇其中的一個時,相應的視圖控制器會出現,頂部有一個UINavigationBar。

我想爲該導航欄添加一個按鈕。我怎樣才能做到這一點?

謝謝。

回答

0

好吧,如果你想以編程方式做到這一點,那麼我建議那所房子的各個視圖控制器UINavigationControllers替換你的UITabBar每個viewControllers的。

所以,你的舊代碼看起來是這樣的:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease]; 
    [window addSubview:tbc.view]; 
    UIViewController *mapVC = [[[UIViewController alloc] init]autorelease]; 
    NSArray *tabViewControllerArray = [NSArray arrayWithObjects:self.mapVC, nil]; 
    tbc.viewControllers = tabViewControllerArray; 
} 

新的代碼應該是:

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    UITabBarController *tbc = [[[UITabBarController alloc]init]autorelease]; 
    [window addSubview:tbc.view]; 
    UIViewController *mapVC = [[[UIViewController alloc] init]autorelease]; 
    // add the viewController to a UINavigationController 
    UINavigationController *mapNav = [[[UINavigationController alloc] initWithRootViewController:mapVC]autorelease]; 
    // put the nav controller in the array instead 
    NSArray *tabViewControllerArray = [NSArray arrayWithObjects:mapNav, nil]; 
    tbc.viewControllers = tabViewControllerArray; 

    // this code adds a right button to the mapBav navigationBar 
    // this uses a custom view, but you could use a standard UIBarButtonItem too 
    NSArray *items = [NSArray arrayWithObjects: [UIImage imageNamed:@"flag-icon.png"], nil]; 
    UISegmentedControl *tableControl = [[[UISegmentedControl alloc] initWithItems:items]autorelease]; 
    tableControl.segmentedControlStyle = UISegmentedControlStyleBar; 
    UIBarButtonItem *segmentBarItem = [[[UIBarButtonItem alloc] initWithCustomView:tableControl] autorelease]; 
    self.navigationItem.rightBarButtonItem = segmentBarItem; 
} 
0

有兩種方法可以解決這個問題。

  1. 只需使用一個UINavigationController爲每個選項卡,與您的視圖控制器在其中。但它聽起來像你不想這樣做,所以:

  2. 把UINavigationBar放入使用Interface Builder。 nav bar http://cl.ly/395fb8c0a9e9df781897/content
    然後,您可以在UIBarButtonItems中拖動並根據自己的喜好對其進行配置,同樣也可以在IB中進行配置。 buttons http://cl.ly/7501d3e8e5d57dac5e00/content

相關問題