2011-05-25 135 views
7

我與導航欄按鈕的工作items.i使用下面的代碼,這樣做添加欄按鈕項目

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc] 
            initWithTitle:@"Save"            
            style:UIBarButtonItemStyleBordered 
            target:self 
           action:@selector(save_Clicked:)]; 
    self.navigationItem.rightBarButtonItem = btnSave; 
    [btnSave release]; 

    UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] 
            initWithTitle:@"Cancel"            
            style:UIBarButtonItemStyleBordered 
            target:self 
            action:@selector(save_Clicked)]; 
    self.navigationItem.leftBarButtonItem = btnCancel; 
    [btnCancel release]; 

我的問題是如何添加另一個按鈕僅僅靠近左邊欄按鈕項目。 在此先感謝

回答

8

要做到這一點,你需要創建一個工具欄,然後不斷加入的UIButton它,然後將工具欄作爲leftBarButton

是這樣的:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)]; 
tools.tintColor = [UIColor clearColor]; 
[tools setTranslucent:YES]; 

NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:9]; 

UIImage *myImage = [UIImage imageNamed:@"AL_HomeMod_Icon.png"]; 
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewHomeMod) forControlEvents:UIControlEventTouchUpInside]; 

UIBarButtonItem *bi = [[UIBarButtonItem alloc] 
         initWithCustomView:myButton]; 

[buttons addObject:bi]; 
[bi release]; 

myImage = [UIImage imageNamed:@"AL_History_Icon.png"]; 
myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewHistory) forControlEvents:UIControlEventTouchUpInside]; 

bi = [[UIBarButtonItem alloc] 
     initWithCustomView:myButton]; 

[buttons addObject:bi]; 
[bi release]; 

myImage = [UIImage imageNamed:@"AL_RX_Icon.png"]; 
myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewCustomPopView2) forControlEvents:UIControlEventTouchUpInside]; 

bi = [[UIBarButtonItem alloc] 
     initWithCustomView:myButton]; 

[buttons addObject:bi]; 
[bi release]; 

myImage = [UIImage imageNamed:@"AL_User_Icon.png"]; 
myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[myButton setImage:myImage forState:UIControlStateNormal]; 
myButton.showsTouchWhenHighlighted = YES; 
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height); 

[myButton addTarget:self action:@selector(clickViewCustomPopView:) forControlEvents:UIControlEventTouchUpInside]; 
bi = [[UIBarButtonItem alloc] 
     initWithCustomView:myButton]; 
[buttons addObject:bi]; 
popButton = myButton; 
[bi release]; 


// stick the buttons in the toolbar 
[tools setItems:buttons animated:NO]; 

[buttons release]; 

// and put the toolbar in the nav bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release]; 

希望幫助

Pondd

1

使用兩個按鈕創建自定義視圖,並使用UIBarButtonIteminitWithCustomView:初始值設定項。這應該做到這一點。

2

我通過使用下面的代碼實現了我的任務:

UIToolbar *tools=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 150, 44)]; 

tools.backgroundColor=[UIColor clearColor]; 

[tools setTranslucent:YES]; 

UIBarButtonItem *optionBtn=[[UIBarButtonItem alloc]initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:self action:@selector(optionPressed:)]; 

UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(donePressed:)]; 

NSArray *buttons=[NSArray arrayWithObjects:optionBtn,doneBtn, nil]; 

    [tools setItems:buttons animated:NO]; 

self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:tools]; 

注意:從IOS 5.0起,蘋果已經做得更容易了。這是可以做到的

self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:optionBtn,doneBtn, nil]; 
7

創建一個按鈕,

UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
           initWithImage:[UIImage imageNamed:@"logout.png"] 
           style:UIBarButtonItemStylePlain 
           target:self action:@selector(doLogout)]; 

添加該按鈕,導航欄

self.navigationItem.rightBarButtonItem = logoutButton; 

的權利或添加此按鈕,導航欄的左側

self.navigationItem.leftBarButtonItem = logoutButton; 

doLogout是一個功能,將在觸摸註銷按鈕上調用

相關問題