从虚空中调用IBAction

时间:2014-02-25 02:35:11

标签: ios uibutton nstimer

这是我的代码。我希望当计时器达到0时按钮会改变,但它不起作用:

-(void) SwitchView{

    [switchingTimer invalidate];
    self.SwitchingTimer = nil;
    [self bomb:nil];
}

-(IBAction)bomb:(UIButton *)sender {
    UIButton *button = (UIButton *)sender;
    [button setImage:[UIImage imageNamed:@"black_bomb.png"] forState:UIControlStateNormal];
    int xmin = ([button frame].size.width)/2;
    int ymin = ([button frame].size.height)/2;

    int x = xmin + arc4random_uniform(self.view.frame.size.width - button.frame.size.width);
    int y = ymin + arc4random_uniform(self.view.frame.size.height - button.frame.size.height);
    [button setCenter:CGPointMake(x, y)];


    [self flower:sender];
}

-(IBAction)flower:(UIButton *)sender{

    UIButton *button2 = (UIButton *)sender;
    [button2 setImage:[UIImage imageNamed:@"red_rose_flower_6.png"] forState:UIControlStateNormal];
    int xmin = ([button2 frame].size.width)/2;
    int ymin = ([button2 frame].size.height)/2;
    int x = xmin + arc4random_uniform(self.view.frame.size.width - button2.frame.size.width);
    int y = ymin + arc4random_uniform(self.view.frame.size.height - button2.frame.size.height);
    [button2 setCenter:CGPointMake(x, y)];
if (self.switchingTimer == nil) {
    self.switchingTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(SwitchView) userInfo:nil repeats:YES];
}

1 个答案:

答案 0 :(得分:3)

你正在打电话 [self bomb:nil];
发件人nil

bomb方法使用发件人:

-(IBAction)bomb:(UIButton *)sender {
    UIButton *button = (UIButton *)sender;

但是来电为nil发送sender

UIButton来电中提供bomb指针 或者不要使用sender而是使用UIButton指针。

UIButton指针可以来自IBOutlet