NSInvocation:invocationWithMethodSignature:
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


(C) 2000-2007 Satoshi Oomori.
[Apple]

Google
Webwww.oomori.com
Apple_Store_40x120

[ad:Usual day in Japan]

この記事を評価してください。 

良い 

間違いがある 説明がわかりにくい 例文がわかりにくい  

Home 目次>Foundation>NSInvocation
修正日2007.3.27