认识到在UINavigationBar中点击标题

时间:2015-04-01 04:39:20

标签: ios objective-c

当用户点击导航栏中的标题时,有人可以帮助我识别一下,在Objective-C中吗?

4 个答案:

答案 0 :(得分:2)

尝试以下方法:

1)添加按钮作为导航的标题视图

UIButton *titleLabelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[titleLabelButton setTitle:@"myTitle" forState:UIControlStateNormal];
titleLabelButton.frame = CGRectMake(0, 0, 70, 44);
titleLabelButton.font = [UIFont boldSystemFontOfSize:16];
[titleLabelButton addTarget:self action:@selector(didTapTitleView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = titleLabelButton;

2)实现选择器

- (IBAction)didTapTitleView:(id) sender{
    //Perform your actions
    NSLog(@"Title tap");
}

答案 1 :(得分:1)

您无法直接在title的{​​{1}} UINavigationItem属性上触摸事件。

有一个属性UINavigationBar,您可以在其中设置并触摸事件。因此,您可以制作titleView并将其设置为UILabel

UINavigationItem

然后,您可以在此[self.navigationItem setTitleView:label]; 上设置点击事件。

label

答案 2 :(得分:1)

UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]
                                            initWithTarget:self
                                            action:@selector(hideKeyBoard)];

    tapGesture.cancelsTouchesInView = NO;
    [self.navigationController.view addGestureRecognizer:tapGesture];


-(void)hideKeyBoard
{
    [self.view endEditing:YES];
}

答案 3 :(得分:0)

您需要使用自定义标题视图并为其添加点击手势识别器。

viewDidLoad

中试试
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(navTitleTapped)];

UILabel* navigationTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 500, 50)];
navigationTitleLabel.text = @"The title goes here...";
[navigationTitleLabel addGestureRecognizer:tap];

self.navigationItem.titleView = navigationTitleLabel;

然后,实施navTitleTapped

-(void)navTitleTapped
{
    //do what you need to do
}