Come posso passare più di un parametro al selettore in 'ScheduledtimerWithtimeInterval: Target: Selector: UserInFO: Ripeti:'

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

Domanda

So che le informazioni dell'utente vengono utilizzate per passare un parametro, ma come faccio passare più di uno di questi?

Immagino che devo usare un oggetto, ma come sono abbastanza nuovo per obiettivo-c, non so davvero se è giusto e come farlo?

Grazie!

È stato utile?

Soluzione

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"];
    // ...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top