想要在navigation controller 的顶端条添加一个刷新键。不知道为什么不执行。
hearder代码:
@interface PropertyViewController : UINavigationController {}
添加button代码:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain
target:self action:@selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
}
return self;
}
不知道错误在哪儿,多谢指教
试试在viewDidLoad
中实现。一般来说,代码都应该写在这个函数里面,因为一个UIViewController
在初始化了之后并不一定会显示出来,在这种情况下viewDidLoad
是不会被调用的
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
}
self.navigationItem.rightBarButtonItem= anotherButton; [anotherButton release];}
添加一个button
到view controller
中的navigationItem
。并定义在你创建的PropertyViewController
类中。
MainViewController *vc=[[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
vc.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];
PropertyViewController *navController = [[PropertyViewController alloc] initWithRootViewController:vc];
这样创建的infoButton 出现在导航bar里。导航控制器会通过UIViewController 选择显示的信息(题目,button),实际上没有创建buttons,直接显示UINavigationController 。
获取到导航控制栏之后 直接添加rightbuttonitem
1楼的回答 很受用 也解决了我的问题