以下方法无法识别 UIButton 选择器

UIButton selector not recognized by the following method

我在选择器中声明了一个方法,并创建了一个同名方法。然而,该方法表示它无法识别。错误信息:

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

    UIImage *btnImage = [UIImage imageNamed:@"ic_favorite_border_48pt.png"];
    UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];

    // get the image size and apply it to the button frame
    CGRect listButtonFrame = heart.frame;
    listButtonFrame.size = btnImage.size;
    heart.frame = listButtonFrame;

    [heart setImage:btnImage forState:UIControlStateNormal];
    [heart addTarget:self.navigationController.parentViewController
                   action:@selector(revealToggle:)
         forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *jobsButton =
    [[UIBarButtonItem alloc] initWithCustomView:heart];

    self.navigationItem.rightBarButtonItem = jobsButton;


    -(void)revealToggle:(UIButton *)heart
    {

    }

您已将 revealToggle 方法放入 viewDidAppear 方法中。你不能那样做。把它移到外面。

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

    UIImage *btnImage = [UIImage imageNamed:@"ic_favorite_border_48pt.png"];
    UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];

    // get the image size and apply it to the button frame
    CGRect listButtonFrame = heart.frame;
    listButtonFrame.size = btnImage.size;
    heart.frame = listButtonFrame;

    [heart setImage:btnImage forState:UIControlStateNormal];
    [heart addTarget:self
                   action:@selector(revealToggle:)
         forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *jobsButton =
    [[UIBarButtonItem alloc] initWithCustomView:heart];

    self.navigationItem.rightBarButtonItem = jobsButton;
}

-(void)revealToggle:(UIButton *)heart
{

}

并且由于 revealToggle 位于同一 class 内,您需要将 self 作为目标传递。