| Home 目次>Foundation>NSInvocation | |
invocationWithMethodSignature:
メソッドシグネチャで起動オブジェクト(NSInvocation)を作って返します
+(NSInvocation *) invocationWithMethodSignature:(NSMethodSignature *)signature
【返り値】 | |
| NSInvocation * | 起動オブジェクト |
| 【パラメータ】 | |
| signature | メソッドシグネチャ |
【解説】
メソッドシグネチャで起動オブジェクト(NSInvocation)を作って返します。
起動オブジェクト(NSInvocation)を起動する前に、アクションセレクタとターゲット、引数をセットしておきます。
メソッドシグネチャとは、引数と返り値のセット
たとえば-(BOOL)updateAppointmentsForDate:(Date *)aDateというメソッドの場合
メソッドシグネチャはBOOLとDate *
【例文】
#import "MyObject.h"
@implementation MyObject
NSTimer *timer= nil ;
NSInvocation * invocation ; // 起動オブジェクト
- ( IBAction )myAction:( id )sender
{
// 呼び出すメソッドをコロコロ変えるサンプル
NSMethodSignature * aSignature ; // メソッドシグネチャ
SEL aSelector = @selector ( testSelector1: ); // セレクタをセット
aSignature = [ self methodSignatureForSelector:aSelector ]; // セレクタのシグネチャをセット
invocation = [ NSInvocation invocationWithMethodSignature:aSignature ]; // 起動オブジェクトをセット
NSLog([aSignature description]);
// 起動オブジェクトにターゲットと引数をセットする
[ invocation setTarget: self ]; // ターゲットは self
[ invocation setSelector: aSelector ]; // セレクタをセット
[ invocation invoke ]; // 起動する
}
-( int ) testSelector1:(NSString *)string
{
NSLog( @"...call testSelector1" );
// やっぱり testSelector2: メソッドを呼び出すことにする
[invocation setSelector: @selector ( testSelector2: )];
// タイマーで 1 秒後に invocation オブジェクを起動
timer = [NSTimer scheduledTimerWithTimeInterval: 1
invocation: invocation
repeats: NO ];
return 1 ;
}
-( int ) testSelector2:(NSString *)string
{
NSLog( @"...call testSelector2" );
// やっぱり testSelector1: メソッドを呼び出すことにする
[invocation setSelector: @selector ( testSelector1: )];
// タイマーで 1 秒後に invocation オブジェクを起動
timer = [NSTimer scheduledTimerWithTimeInterval: 1
invocation: invocation
repeats: NO ];
return 2 ;
}
@end
![]() | |
この記事を評価してください。
| Home 目次>Foundation>NSInvocation | 修正日2007.3.27 |