我已经用不同类型的代码奋斗了几个小时,以将自定义项目添加到我的导航栏。不幸的是它失败了,并且没有调试错误。现在我只能更改栏的标题。
代码添加到特定ViewController的viewDidLoad方法中:
UIBarButtonItem *gotoNext = [[UIBarButtonItem alloc] initWithTitle:@"Goto next" style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextAction)];
self.navigationItem.rightBarButtonItem = gotoNext;
我尝试了以下代码,但不幸的是没有任何运气:
UIView* container = [[UIView alloc] init];
UIButton * button = [[UIButton alloc] init];
[button setTitle:@"Test" forState:UIControlStateNormal];
[container addSubview:button];
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
self.navigationItem.rightBarButtonItem = item;
最佳答案
我发现您的代码唯一的问题是选择器名称末尾缺少冒号。它应该是:
UIBarButtonItem *gotoNext = [[UIBarButtonItem alloc] initWithTitle:@"Goto next" style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextAction:)];
关于ios - 如何将自定义项目添加到导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10543473/