我添加了一个UIBarButton。但是,我无法显示标题。
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];
[button addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;
我尝试了以下内容:
button.title =@"Title";
和
customItem.title=@"title";
答案 0 :(得分:1)
不需要像你一样做那么多东西,就像这样:
UIBarButtonItem *customItem=[[UIBarButtonItem alloc]initWithTitle:@"Title" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonClicked:)];
self.navigationItem.rightBarButtonItem=customItem;
希望它有帮助.... :)
答案 1 :(得分:0)
使用UIButton setTitle:forState:
代替访问属性。您还应该使用方法[UIButton buttonWithType:<#(UIButtonType)#>]
而不是UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];
答案 2 :(得分:0)
使用以下代码
UIBarButtonItem *customItem = [[UIBarButtonItem alloc]
initWithTitle:@"Title"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(doneButtonClicked:)];
self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;