1
中的選擇器我知道用戶信息用於傳遞參數,但是如何傳遞多個參數?如何將多個參數傳遞給'scheduledTimerWithTimeInterval:target:selector:userInfo:'重複:'
我猜我必須使用一個對象,但因爲我對objective-c相當陌生,我不知道這是否正確以及如何去做?
謝謝!
中的選擇器我知道用戶信息用於傳遞參數,但是如何傳遞多個參數?如何將多個參數傳遞給'scheduledTimerWithTimeInterval:target:selector:userInfo:'重複:'
我猜我必須使用一個對象,但因爲我對objective-c相當陌生,我不知道這是否正確以及如何去做?
謝謝!
用你需要傳遞的多個對象創建一個包裝對象或NSDictionary
並傳遞userInfo
中的包裝對象。在接收器上從包裝器對象中檢索對象。
示例代碼:
呼叫號碼:
NSString *obj1 = @"string1";
NSString *obj2 = @"string2";
NSDictionary *wrapper = [NSDictionary dictionaryWithObjectsAndKeys:obj1, @"Object1", obj2, @"Object2", nil];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:wrapper repeats:NO];
接收定時器的代碼:
- (void)timerFireMethod:(NSTimer*)theTimer {
NSDictionary *wrapper = (NSDictionary *)[theTimer userInfo];
NSString * obj1 = [wrapper objectForKey:@"Object1"];
NSString * obj2 = [wrapper objectForKey:@"Object2"];
// ...
}