¿Cómo paso más de un parámetro al selector en 'ScheduledTimerWithTimeInterval: Destale: Selector: UserInfo: Repite:'

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

Pregunta

Sé que se usa la información del usuario para pasar un parámetro, pero ¿cómo paso más de uno de estos?

Supongo que tengo que usar un objeto, pero como soy bastante nuevo para Objetivo, realmente no sé si esto es correcto y cómo hacerlo.

¡Gracias!

¿Fue útil?

Solución

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"];
    // ...
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top