2015-02-09 43 views
-2

我試圖創建一個NSTimer,我可以在其中傳遞不同的UILabels,它將根據時間更改文本顏色。以下是我使用的代碼示例,希望能夠實現這一點。我確信它會工作,但只要定時器啓動我的應用程序崩潰與下面的日誌。將參數傳遞給NSTimer中的方法時,應用程序崩潰

爲什麼會發生這種情況?我認爲我已經正確設置了一切。

.H

IBOutlet UILabel *titleColor; 
int time; 
NSTimer *timer; 

.M

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

- (void)timeCycle:(UILabel *)label { 
    time++; 
    if (time == 10) { 
     time = 0; 
    } 

    [self labelColorCycle:label]; 
} 

- (void)labelColorCycle:(UILabel *)label { 

    if (time == 0) { 
     [label setTextColor:[UIColor colorWithRed:100 
            green:50 
            blue:75 
           alpha:1]]; 
    } 
    else if (time == 1) { 
     // sets another color 
    } 
} 

崩潰報告

2015-02-09 17:17:51.454 Test App[404:30433] -[__NSCFTimer setTextColor:]: unrecognized selector sent to instance 0x14695bb0 
2015-02-09 17:17:51.455 Test App[404:30433] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer setTextColor:]: unrecognized selector sent to instance 0x14695bb0' 
*** First throw call stack: 
(0x228c849f 0x300c2c8b 0x228cd8b9 0x228cb7d7 0x227fd058 0xf70e5 0xf6f17 0x235d9ba1 0x2288ec87 0x2288e803 0x2288ca53 0x227da3c1 0x227da1d3 0x29bd80a9 0x25de87b1 0xf5d41 0x30642aaf) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
+2

的參數'timeCycle:'*必須*是'NSTimer',而不是一些標籤。 – rmaddy 2015-02-09 22:34:34

+0

可能重複[如何調試'無法識別的選擇器發送到實例'錯誤](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error ) – 2015-02-09 22:36:22

+1

另一方面,labelColorCycle的參數必須是UILabel,而不是NSTimer。 – 2015-02-09 22:38:53

回答

3

timeCycle:的說法是不NSTimerUILabel。還有爲什麼你需要通過標籤?這是一個財產權?

因此改變你的方法,如:

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

- (void)timeCycle:(NSTimer *)timer 
{ 
    time++; 
    if (time == 10) 
    { 
     time = 0; 
    } 

    [self labelColorCycle:self.titleColor]; 
} 

- (void)labelColorCycle:(UILabel *)label 
{ 

    if (time == 0) 
    { 
     [label setTextColor:[UIColor colorWithRed:100 green:50 blue:75 alpha:1]]; 
    } 
    else if (time == 1) 
    { 
     // sets another color 
    } 
} 

如果你需要通過標籤作爲參數使用的NSTimeruserInfo財產。

在這種情況下,你的代碼會像:

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

- (void)timeCycle:(NSTimer *)timer 
{ 
    time++; 
    if (time == 10) 
    { 
     time = 0; 
    } 

    [self labelColorCycle:(UILabel *)[timer userInfo]]; 
} 

- (void)labelColorCycle:(UILabel *)label 
{ 

    if (time == 0) 
    { 
     [label setTextColor:[UIColor colorWithRed:100 green:50 blue:75 alpha:1]]; 
    } 
    else if (time == 1) 
    { 
     // sets another color 
    } 
} 
+0

謝謝。這將爲我節省大量的時間和代碼行。 – user3781632 2015-02-09 23:03:19

+1

@ user3781632:不客氣。快樂的編碼! :) – 2015-02-09 23:09:11

+0

請注意,計時器調用的userInfo屬性應該是一個字典。這就是它的設計。您可能可以使用不同的對象類型,但不推薦。 – 2015-02-10 01:16:24