'ScheduleDtimerWithTimeInterval:target:selector:repeats:'のセレクタに複数のパラメータを渡すにはどうすればよいですか。
-
14-11-2019 - |
質問
私はユーザー情報がパラメータを渡すために使用されるのを知っていますが、これらのうちの1つ以上をどのように渡すのですか?
私はオブジェクトを使わなければならないと思いますが、私はObjective-Cにかなり新しいように私はこれが正しいかどうか、そしてそれについてどのように行くかどうかはわかりませんか?
ありがとう!
解決
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