2015-02-09 59 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

相關問題