嗨我需要在導航欄中以編程方式在右側設置按鈕,這樣如果我按下按鈕,我將執行一些操作。我用編程方式創建了導航欄;以編程方式將按鈕添加到導航欄
navBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0,0,320,44) ];
同樣,我需要添加按鈕,在此導航欄的右側。爲此,我使用的,
1.
UIView* container = [[UIView alloc] init];
// create a button and add it to the container
UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 130, 44.01)];
[container addSubview:button];
[button release];
// add another button
button = [[UIButton alloc] initWithFrame:CGRectMake(160, 0, 50, 44.01)];
[container addSubview:button];
[button release];
// now create a Bar button item
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;
[item release];
2.
UIImage *im;
im=[UIImage imageNamed:@"back.png"];
[button setImage:im forState:UIControlStateNormal];
[im release];
backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[backButton setImageInsets:UIEdgeInsetsMake(0, -10,5, 5)];
[self.navigationItem setRightBarButtonItem:backButton];
3.
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"button" style:UIBarButtonItemStylePlain target:self action:@selector(refreshLogsAction:)];
self.navigationItem.rightBarButtonItem = refreshItem;
[refreshItem release];
我嘗試了所有這些方法,但沒有一個在右側顯示按鈕。
我需要一個冒號添加到選擇 - 「行動:@選擇(flipView :)]; – Rydell 2012-11-06 17:03:14