带有NSNotificationCenter的NSInvalidArgumentException自定义按钮

时间:2015-07-22 00:16:16

标签: ios objective-c

我正在尝试创建一个自定义按钮,并将其类作为观察者添加到自定义初始值设定项中的NSNotificationCenter中。

@implementation SACenterDiskButton

-(id)initWithImage:(UIImage *)image forView:(UIView *)view;
{
    self = [super init];
    if (self) {
        self = [UIButton buttonWithType:UIButtonTypeCustom];
        self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
        double buttonWidth = view.frame.size.width/5;
        self.frame = CGRectMake(0.0, 0.0, buttonWidth, 57);
        self.adjustsImageWhenHighlighted = NO;
        self.imageView.clipsToBounds = NO;
        self.imageView.contentMode = UIViewContentModeCenter;

        [self setImage:image forState:UIControlStateNormal];
        [self setBackgroundColor:[UIColor colorWithRed:0.317 green:0.4 blue:0.544 alpha:1]];

        [view addSubview:self];

        //Add observers for when tracks are played/paused/ended for spinning the center button
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(trackPlayedNotification:) name:@"trackPlayed" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playPauseNotification:) name:@"togglePlayPause" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:@"songEnded" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
    }
    return  self;
}

#pragma mark - Notificaton Center

-(void)applicationWillEnterForeground:(NSNotification *)notification
{
    BOOL playerIsPlaying = [PlayerManager sharedManager].playerIsPlaying;

    if (playerIsPlaying) {

        [self.imageView rotate360WithDuration];
    }
}

-(void)itemDidFinishPlaying:(NSNotification *)notification
{

    [self.imageView stopAllAnimations];

}

-(void)trackPlayedNotification:(NSNotification *)notification
{
    [self.imageView rotate360WithDuration];
}

-(void)playPauseNotification:(NSNotification *)notification
{

    NSNumber *playerIsPlaying = notification.userInfo[@"playerIsPlaying"];

    if ([playerIsPlaying boolValue]) {

        [self.imageView pauseAnimations];
    }
    else {

        [self.imageView resumeAnimations];
    }
}

该按钮运行良好,除非发送通知时,我收到此错误。

-[UIButton applicationWillEnterForeground:]: unrecognized selector sent to instance 0x17dd8930

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton trackPlayedNotificaion:]: unrecognized selector sent to instance 0x17dd8930'
*** First throw call stack:

我是否正确地将观察员添加到班级?我有什么想法让这次崩溃?

1 个答案:

答案 0 :(得分:0)

问题在于以下几行:

self = [UIButton buttonWithType:UIButtonTypeCustom];

这会将self重新分配给新的UIButton实例,替换所需的自定义按钮类的新实例。

只需删除该行,您的代码就会按预期工作。