'scheduledtimerwithtimeInterval : 대상 : 선택기 : userinfo : 반복 :'에서 셀렉터에 하나 이상의 매개 변수를 전달하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/9515591

문제

사용자 정보가 매개 변수를 전달하는 데 사용되는 것을 알고 있지만 이들 중 둘 이상을 어떻게 통과합니까?

나는 물체를 사용해야 만합니다.

감사합니다!

도움이 되었습니까?

해결책

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"];
    // ...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top