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

时间:2018-03-14 03:44:17

标签: ios objective-c uibutton selector

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

code

- (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
    {

    }

1 个答案:

答案 0 :(得分:6)

您已将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位于同一个类中,因此您需要将self作为目标。