我想在导航标题的标题中添加动作。(示例"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: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
复制相似问题