OStack程序员社区-中国程序员成长平台

标题: ios - 在导航栏上作为 UIBarButtonItem 放置的 UIButton 在点击时不会触发方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 14:38
标题: ios - 在导航栏上作为 UIBarButtonItem 放置的 UIButton 在点击时不会触发方法

我创建了一个自定义 UIButton 并将其放置在导航 Controller 的右侧。但是当我点击它时,它并没有触发buttonTapped方法并打印输出Testing 123,我做错了什么?

- (void)viewDidAppearBOOL)animated {
    [super viewDidAppear:animated];

    UIImage *btnImage = [[UIImage alloc] init];
    BOOL unlike = NO;

    if (unlike){
        btnImage = [UIImage imageNamed"ic_favorite_border_48pt.png"];
    }else{
        btnImage = [UIImage imageNamed"ic_favorite_48pt.png"];
    }

    UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];

    CGRect listButtonFrame = heart.frame;
    listButtonFrame.size = btnImage.size;
    heart.frame = listButtonFrame;

    [heart setImage:btnImage forState:UIControlStateNormal];
    [heart addTarget:self.navigationController.parentViewController
              actionselector(buttonTapped forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *jobsButton = [[UIBarButtonItem alloc] initWithCustomView:heart];
    self.navigationItem.rightBarButtonItem = jobsButton;
}

-(void)buttonTappedUIButton*)heart{
    NSLog(@"Testing 123");
}

@end



Best Answer-推荐答案


[heart addTarget:self.navigationController.parentViewController
           actionselector(buttonTapped
 forControlEvents:UIControlEventTouchUpInside];

可能需要

[heart addTarget:self
           actionselector(buttonTapped
 forControlEvents:UIControlEventTouchUpInside];

关于ios - 在导航栏上作为 UIBarButtonItem 放置的 UIButton 在点击时不会触发方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49270807/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4