信息按钮作为UIBarButtonItem无法响应点击

3

我正在尝试以编程方式向我的应用程序导航栏添加信息按钮。然而,它从来没有触发我注册的操作。

UIBarButtonItem* infoButton =[[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeInfoLight]];
    [infoButton setTarget:self];
    [infoButton setAction:@selector(infoButtonTapped)];

按钮出现在我的导航栏中,但点击它不会传递到infoButtonTapped函数。
1个回答

11

将目标/操作添加到UIButton中。当从UIButton设置它时,条形按钮项的行为会有所不同。

UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:@selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接