【问题标题】:UIButton selector not recognized by the following method以下方法无法识别 UIButton 选择器
【发布时间】:2018-03-14 03:44:17
【问题描述】:

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

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

    }

【问题讨论】:

  • Objective-C 不支持嵌套方法。

标签: ios objective-c uibutton selector


【解决方案1】:

您已将 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 作为目标传递。

【讨论】:

  • 谢谢 rmaddy,你很敏锐。没想到我的方法在viewDidAppear里面,之前很疑惑。
  • 我没看到 +1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-22
  • 2021-04-27
  • 2021-11-10
  • 2017-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多