2010-07-25 140 views
0

我試圖將3個自定義按鈕添加到我的視圖頂部的導航控制器工具欄中,或者添加3個選項的分段控件。當我創建我的視圖控制器(fwc)但按鈕不出現時,我的應用程序委託上有以下代碼。將自定義按鈕添加到導航控制器

/* 設置導航控制器的進給翼片 */

// instantiate the feedingViewController and set the title to Feedings 
feedingViewController *fwc = 
[[feedingViewController alloc] initWithNibName:@"feedingViewController" 
              bundle:[NSBundle mainBundle]]; 
//fwc.title = @"Feedings"; 

// set the tab bar item up and add it as feedingViewController's tab bar item 
UITabBarItem *feedingTabBarItem = 
[[UITabBarItem alloc] initWithTitle:@"Feedings" image:nil tag:0]; 
fwc.tabBarItem = feedingTabBarItem; 
[feedingTabBarItem release]; 

// create a new nav controller for feedings and add root view 
feedingNavController = [[UINavigationController alloc] init]; 

//Create the add button, need to change the selector to something though ***** 
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                    target:self 
                    action:@selector(newFeeding)]; 
//self.navigationItem.rightBarButtonItem = add; 


UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc] 
              initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
              target:nil action:nil]; 

// Create and configure the segmented control 
UISegmentedControl *sortToggle = [[UISegmentedControl alloc] 
            initWithItems:[NSArray arrayWithObjects:@"Ascending",@"Descending", nil]]; 

sortToggle.segmentedControlStyle = UISegmentedControlStyleBar; 

sortToggle.selectedSegmentIndex = 0; 

[sortToggle addTarget:self action:@selector(toggleSorting:)forControlEvents:UIControlEventValueChanged]; 

// Create the bar button item for the segmented control 
UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc]initWithCustomView:sortToggle]; 

[sortToggle release]; 


// Set our toolbar items 
feedingNavController.toolbarItems = [NSArray arrayWithObjects: 
        flexibleSpaceButtonItem, 
        sortToggleButtonItem, 
        flexibleSpaceButtonItem, 
        add,      
        nil]; 
feedingNavController.navigationController.navigationBarHidden=NO; 

[sortToggleButtonItem release]; 
[add release]; 

// Push the feedingViewController on the nav stack and release it. 
[feedingNavController pushViewController:fwc animated:NO]; 
[fwc release]; 
+0

你爲什麼註釋掉該行自.navigationItem.rightBarButtonItem =添加? – 2010-07-25 03:08:57

回答

1

爲了使用中的UITabBar一樣,你需要一個的UITabBarController,這比UINavigationController的不同。 UITabBar與UISegmentedControl有着根本不同的用途。看起來您嘗試實現的功能不適用於UITabBar。在您的問題描述中,您提到嘗試將這些按鈕添加到「頂部的導航控制器工具欄」中。一個UINavigationController有一個UINavigationBar,它是一個橫跨頂部的橫條,還有一個UIToolbar,它是出現在底部的橫條。默認情況下,UIToolbar被設置爲隱藏,但是當您創建UINavigationController時(參見Xcode中的UINavigationController引用),您將獲得一個UIToolbar。

Apple的NavBar演示顯示瞭如何將UISegmentedControl放入UINavigationBar。取而代之的是標題,使用自定義titleview的顯示分段控制:

fwc.navigationItem.titleView = sortToggle; 

如果你想放置在UINavigationBar的外接的UIBarButtonItem,以及,你可以使用:

fwc.navigationItem.rightBarButtonItem = add; 

請注意,您實際上不應該試圖自己定製UINavigationController的導航欄。定製的正確方法是讓一個單獨的視圖控制器訪問它自己的navigationItem,然後用你想要的項目設置titleView和rightBarButtonItem。

如果你想使用UIToolBar代替,這意味着你的項目將出現在屏幕的底部接近你的問題,你可以做這樣的事情:

// Assume UIBarButtonItem *add, UIBarButtonItem *sortToggleButtonItem, 
// and UIBarButtonItem *flexibleSpaceButtonItem are allocated 
[fwc setToolbarItems:[NSArray arrayWithObjects: 
       flexibleSpaceButtonItem, 
       sortToggleButtonItem, 
       flexibleSpaceButtonItem, 
       add,      
       nil]]; 
[feedingNavController setToolbarHidden:NO]; 
+0

感謝您的回答,您提出的建議確實有效,但我寧願將UI元素放在頂部。我決定在視圖頂部添加一個分段控件並使用它。不過謝謝。 – adam0101 2010-07-25 17:50:38