macOS/iOS API解説

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

目次

initWithFireDate:interval:target:selector:userInfo:repeats:

重複?起動する時、時間間隔でタイマーを作って返します
-(id)initWithFireDate:(NSDate *)date:
           interval:(NSTimeInterval)seconds:
           target:(id)target:
           selector:(SEL)aSelector:
           userInfo:(id)userInfo:
           repeats:(BOOL)repeats:

解説

起動する時、時間間隔でタイマーを作って返します。

返り値

( id )

タイマー

引数

( NSDate * )date

タイマーを起動する日付

( NSTimeInterval )seconds

時間間隔

( id )target

ターゲット

( SEL )aSelector

セレクタ

( id )userInfo

追加情報

( BOOL )repeats

繰り返すか

クラス

NSTimer

Class Methods

使用可能

10.2

参照

例文

#import "MyObject.h"
@implementation MyObject

NSTimer *timer;

- (IBAction)myAction:(id)sender
{
	//現在の時間
	NSDate *theDate = [NSDate date];    
	//NSDate *theDate = [NSDate dateWithString:@"2015-09-02 13:31:00 +0900"];     
	
	//userInfoに使う辞書を作成
	NSDictionary *userInfoDictionary =[NSDictionary dictionaryWithObjectsAndKeys:
						@"value1",@"key1",
						@"value2",@"key2",
						@"value3",@"key3",nil];

	
	timer = [[NSTimer alloc] initWithFireDate:theDate
							interval: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
{
	//動いていたら無効に
	if ([timer isValid]){
		[timer invalidate];
	}
}

-(void) timerControl:(NSTimer *)aTimer
{
NSLog(@"...%@",[[aTimer userInfo] objectForKey:@"key2"]);
}
@end