2011-10-03 86 views
1

我正在開發一個小應用程序。我需要在子類中創建三個按鈕。一個按鈕是add,另一個是search,最後一個是back。我還創建了左右按鈕。但我無法在導航欄的中心創建搜索按鈕。我如何創建它?我的代碼是:如何以編程方式在iPhone的UINavigationbar中創建三個按鈕?

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle:@"Flip"            
            style:UIBarButtonItemStyleBordered 
           target:self 
           action:@selector(flipView)]; 
self.navigationItem.rightBarButtonItem = flipButton; 
[flipButton release]; 

UIBarButtonItem *flipButtons = [[UIBarButtonItem alloc] 
           initWithTitle:@"Add"     
           style:UIBarButtonItemStyleBordered 
           target:self 
           action:@selector(addbuttonview)]; 
self.navigationItem.leftBarButtonItem = flipButtons; 
[flipButtons release]; 

} 

如何在導航欄中創建中間按鈕?請幫幫我。

+1

我建議你使用'UISegmentedControl' – Nekto

+0

好的使用這個。但如何在導航欄中使用 –

回答

6

下面是用分段控制在導航欄的代碼編程

NSArray* arr = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"Log_Button.png"], [UIImage imageNamed:@"Chart_Button.png"], nil]; 
segmentedControl = [[UISegmentedControl alloc] initWithItems:arr]; 
[segmentedControl addTarget:self action:@selector(action) forControlEvents:UIControlEventValueChanged]; 
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar]; 
[arr release]; 
UIBarButtonItem *rb = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; 
[self.navigationItem setRightBarButtonItem:rb]; 
[rb release]; 
3
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom]; 
     btnBack.frame = CGRectMake(10, 4, 100, 50); 
     [btnBack setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"button_back" ofType:@"png"]] forState:UIControlStateNormal];  
     [btnBack addTarget:self action:@selector(btnBackPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.navigationController.navigationBar addSubview:btnBack]; 

UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom]; 
     btnHome.frame = CGRectMake(115, 4, 38, 30); 
     [btnHome setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"header_icon_home" ofType:@"png"]] forState:UIControlStateNormal]; 
     [btnHome addTarget:self action:@selector(btnHomePressed:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.navigationController.navigationBar addSubview:btnHome]; 



UIButton *searchBtn=[UIButton buttonWithType:UIButtonTypeCustom]; 
     searchBtn.frame=CGRectMake(175, 2, 60, 40); 
     [searchBtn addTarget:self action:@selector(seachbtnPressed:) forControlEvents:UIControlEventTouchDown]; 
     [searchBtn setImage:[UIImage imageNamed:@"Search.jpg"] forState:0]; 
     [self.navigationController.navigationBar addSubview:searchBtn]; 

採取三種UIButtons並添加到navigationBar.set幀的圖像按照烏爾設計。

+0

錯誤包含此行「BUTTON_BACK_TAG」未聲明。如何解決這個問題。 –

+0

那些是Prag標記。所以在這裏我們有2個解決方案1)給同一個選擇器不同的標籤2)寫入不同的選擇器 – Srinivas

相關問題