macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

timerWithTimeInterval:target:selector:userInfo:repeats:

タイマーを作って返します
+(NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds:
                 target:(id)target:
                 selector:(SEL)aSelector:
                 userInfo:(id)userInfo:
                 repeats:(BOOL)repeats:

解説

タイマーを作って返します。
secondsで指定した時間(秒)が過ぎた後、ターゲットに起動のメッセージを送ります。
repeatsをYESにすると、タイマーは繰り返して実行されます。NOをセットすると1回だけメッセージを送ります。
追加の情報を送るのにuserInfoを使います。
ユーザーがイベントを送信しているときにタイマーは実行されます。
ランループに登録されるモードがNSEventTrackingRunLoopModeならユーザーがイベントを送信しているときにタイマーは実行されます。
NSModalPanelRunLoopModeならモーダルパネルの時にタイマーは実行されます。

返り値

( NSTimer * )

タイマー

引数

( NSTimeInterval )seconds

タイマーが呼び出される間隔(秒)

( id )target

ターゲット

( SEL )aSelector

呼び出すメソッド

( id )userInfo

パラメータ

( BOOL )repeats

繰り返すかYES/NO

クラス

NSTimer

Class Methods

使用可能

10.0

参照

例文

#import "MyObject.h"
@implementation MyObject

NSTimer *timer;

- (IBAction)myAction:(id)sender
{
	//userInfoに使う辞書を作成
	NSDictionary *userInfoDictionary =[NSDictionary dictionaryWithObjectsAndKeys:
						@"value1",@"key1",
						@"value2",@"key2",
						@"value3",@"key3",nil];


	timer = [NSTimer timerWithTimeInterval:1.0
							target:self
							selector:@selector(timerControl:)
							userInfo:userInfoDictionary
								repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
}

- (IBAction)fire:(id)sender
{

}
- (IBAction)stop:(id)sender
{

}

-(void) timerControl:(NSTimer *)aTimer
{
	NSLog(@"...%@",[[aTimer userInfo] objectForKey:@"key2"]);
	int i;
	i = [myOutlet intValue]+1;
	[myOutlet setIntValue:i];
	[myOutlet display];
}
@end