2015-02-09 57 views
0

我试图在另一个方法中创建一个方法,然后传入我的计时器。我被困在NSTimer的selector部分,不知道如何将我的方法传递给它。我想我可以像往常一样通过它:[self methodHere:parameter]但它不工作。尝试传递参数作为选择器的方法时出错

我得到在viewDidLoad中的bgTimer = [NStimer....线两个警告:

  • !预期标识符
  • !预计 “]”

.M

- (void)viewDidLoad { 
    [super viewDidLoad] 
    timer = [NSTimer scheduledTimerWithTimeInterval:animationDuration target:self selector:@selector([self bgColorCycleWithOutlet:bgColor]) userInfo:nil repeats:YES]; 
} 

- (void)bgColorCycleWithOutlet:(UIButton *)outlet { 

    if (time == 0) { 
     [self bgColorSwatchAnimationRed:232 
            green:152 
            blue:58 
           duration:5 
           outlet:outlet]; 
    } 
} 

- (void)bgColorSwatchAnimationRed:(float)r green:(float)g blue:(float)b duration:(int)d outlet:(UIButton *)o { 
    [o setBackgroundColor:[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:0.5]]; 
    [UIView animateWithDuration:d animations:^{ 
     [o setBackgroundColor:[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]]; 
    }]; 
} 

回答

2

它应该是:

@selector(bgColorCycleWithOutlet:) 
+0

我怎么会在UIButton的参数添加到,虽然?我已经尝试过'bgColorCycleWithOutlet:bgColor',但它在'bgColor'之后要求另一个':'。 'bgColor'是我的头文件中声明的UIButton的名称。 – user3781632 2015-02-09 06:12:55

+0

使用userInfo参数传递UIButton。 Timer将自身作为参数传递给选择器。你的bgColorCycleWithOutlet :(UIButton *)插座应该是bgColorCycleWithOutlet:(NSTimer *)计时器,然后在该方法中使用timer.userInfo来获取你传递的按钮。 – jamihash 2015-02-09 06:24:21

相关问题