MENU
コンテンツ再構築中

Objective-C:NSInvocationでタイマーのセレクタに自身以外の引数を渡す

タイマーのセレクタに引数を渡したい場合のメモ。

INDEX

タイマーのセレクタに自身以外の引数を渡す

タイマーのセレクタ(メソッド名)に、Timer自身以外のオブジェクトを渡したいときは NSInvocation を使用する。

[code]
– (void)createTimer
{
SEL sel = @selector(timerCall:text:);
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:sel];

UILabel *mylabel = [UILabel alloc] int];
NSString *mystr = @”myString”;
[invocation setArgument:(void *)&mylabel atIndex:2];
[invocation setArgument:(void *)&mystr atIndex:3];

[NSTimer scheduledTimerWithTimeInterval:3.0f invocation:invocation repeats:NO];
}

– (void)timerCall:(UILabel *)mylabel text:(NSString *)mystr
{
mylabel.text = mystr;
NSLog(@”label.text:%@”,mylabel.text);
}
[/code]

Please share it!
  • URLをコピーしました!
  • URLをコピーしました!
INDEX