'scheduledtimerwithtimeInterval : 대상 : 선택기 : userinfo : 반복 :'에서 셀렉터에 하나 이상의 매개 변수를 전달하는 방법은 무엇입니까?
-
14-11-2019 - |
문제
사용자 정보가 매개 변수를 전달하는 데 사용되는 것을 알고 있지만 이들 중 둘 이상을 어떻게 통과합니까?
나는 물체를 사용해야 만합니다.
감사합니다!
해결책
Create a wrapper object, an NSArray
or NSDictionary
with the multiple objects you need to pass and pass that wrapper object in userInfo
. On the receiver retrieve the objects from the wrapper object.
Example code using a NSDictionary for the wrapper:
Calling code:
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];
Receiving timer code:
- (void)timerFireMethod:(NSTimer*)theTimer {
NSDictionary *wrapper = (NSDictionary *)[theTimer userInfo];
NSString * obj1 = [wrapper objectForKey:@"Object1"];
NSString * obj2 = [wrapper objectForKey:@"Object2"];
// ...
}
제휴하지 않습니다 StackOverflow