ios - 在导航栏上作为 UIBarButtonItem 放置的 UIButton 在点击时不会触发方法
<p><p>我创建了一个自定义 UIButton 并将其放置在导航 Controller 的右侧。但是当我点击它时,它并没有触发<strong>buttonTapped</strong>方法并打印输出<strong>Testing 123</strong>,我做错了什么?</p>
<pre><code>- (void)viewDidAppear:(BOOL)animated {
;
UIImage *btnImage = [ init];
BOOL unlike = NO;
if (unlike){
btnImage = ;
}else{
btnImage = ;
}
UIButton *heart = ;
CGRect listButtonFrame = heart.frame;
listButtonFrame.size = btnImage.size;
heart.frame = listButtonFrame;
;
[heart addTarget:self.navigationController.parentViewController
action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = [ initWithCustomView:heart];
self.navigationItem.rightBarButtonItem = jobsButton;
}
-(void)buttonTapped:(UIButton*)heart{
NSLog(@"Testing 123");
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>[heart addTarget:self.navigationController.parentViewController
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
</code></pre>
<p>可能需要</p>
<pre><code>[heart addTarget:self
action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在导航栏上作为 UIBarButtonItem 放置的 UIButton 在点击时不会触发方法,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/49270807/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/49270807/
</a>
</p>
页:
[1]