Comment puis-je passer plus d'un paramètre sur le sélecteur dans 'planchedTimerWithTimeVal: cible: sélecteur: userinfo: répète:'
-
14-11-2019 - |
Question
Je sais que les informations utilisateur sont utilisées pour réussir un paramètre, mais comment puis-je passer plus d'une de ces?
Je suppose que je dois utiliser un objet, mais comme je suis assez nouveau à l'objectif-C, je ne sais pas vraiment si cela a raison et comment y aller?
merci!
La solution
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"];
// ...
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow