UINavigationBar 触控

UINavigationBar Touch(UINavigationBar 触控)

本文介绍了UINavigationBar 触控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户点击我的一个视图的导航栏标题时触发一个触摸事件.

I would like to fire an event on touch when a user taps the title of the navigation bar of one of my views.

我不知道是否可以访问 UINavigationBar 的标题视图以便将触摸事件连接到它.

I'm at a bit of a loss on whether I can access the view of the title of the UINavigationBar in order to wire up a touch event to it.

这可能吗?

推荐答案

我找到的解决方案是一个按钮,我使用如下(但我不知道它有多合法"):

The solution I found is a button, I use the following (but I don't know how "legal" it is):

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;

把那段代码放在你设置标题的地方.然后我在其他地方进行了测试:

Put that bit of code where ever you set your title. Then elsewhere I had this to test:

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

在控制台上记录了Title Tap"!

which logged "Title tap" on the console!

我处理此问题的方式可能完全错误,但可能会让您对可以查看的内容有所了解.这当然帮助了我!不过可能有更好的方法.

The way I've gone about this may be completely wrong, but might give you an idea on what you can look in to. It's certainly helped me out! There's probably a better way of doing it though.

这篇关于UINavigationBar 触控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:UINavigationBar 触控

基础教程推荐