我試圖創建一個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
的參數'timeCycle:'*必須*是'NSTimer',而不是一些標籤。 – rmaddy 2015-02-09 22:34:34
可能重複[如何調試'無法識別的選擇器發送到實例'錯誤](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error ) – 2015-02-09 22:36:22
另一方面,labelColorCycle的參數必須是UILabel,而不是NSTimer。 – 2015-02-09 22:38:53