我想在导航标题的标题中添加动作。(示例"Explore")

我想使用这种方法执行一些活动。
我试着跟踪代码,但它不工作。
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Explore" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
//[view addSubview:button];
[self.navigationItem.titleView addSubview:button];
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(@"Clicked on Navigation Title");
}ViewController名称来自presentViewController类型,而不是来自push.navigationController堆栈。
如何使用objective-C代码来实现这一点?
发布于 2020-08-10 15:05:50
默认情况下,titleView为nil。
https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview
你应该用你的按钮来设置标题视图。而不是将其添加为子视图。
self.navigationItem.titleView = button发布于 2020-08-10 15:10:27
将您的代码替换为以下代码:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:@selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Explore" forState:UIControlStateNormal];
self.navigationItem.titleView = button;
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(@"Clicked on Navigation Title");
}https://stackoverflow.com/questions/63335304
复制相似问题