2011-12-27 1 views
0

での作業私はのviewDidLoadで2つの方法を実行しているし、それらの間の画像が、黒い画面を変更しないと10秒後に私は結果だけを参照してくださいここで10秒NSRunLoop

-(void)nextImage{ //charging a random image in the image view 

    index = [[NSArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",nil]; 
    NSUInteger randomIndex = arc4random() % [index count]; 
    NSString *imageName = [index objectAtIndex:randomIndex]; 
    NSLog(@"%@",imageName); 
    self.banner=[UIImage imageNamed:imageName]; 
    self.imageView.image=banner; 
    [imageName release]; 
} 

-(void)horror{ 

    self.banner=[UIImage imageNamed:@"Flo.jpg"]; 
    self.imageView.image=banner; 
    NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],@"/scream.wav"]; 
    SystemSoundID soundID; 
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 
    AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); 
    AudioServicesPlaySystemSound(soundID); 

} 

- (void)viewDidLoad 
{ 

    [self nextImage]; 

    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]]; 

    [self horror]; 

    [super viewDidLoad]; 
} 

ためNSRunLoopを実行しているイム[ホラー]の。反対側では、私は[nextImage]をviewDidLoadで画像の変更を維持するとき、私はNSRunLoopで何かがうまくいかないと思う。

答えて

1

ほとんどの場合、直接実行ループで作業しないでください。 runUntilDate:の方法は、あなたが思う通りではありません。あなたのユースケースについて、あなたはセットアップする必要がありタイマー:

- (void)viewDidLoad 
{ 
    [self nextImage]; 
    [NSTimer scheduledTimerWithTimeInterval: 10.0 target: self selector: @selector(horror) userInfo: nil repeats: NO]; 
    [super viewDidLoad]; 
} 

タイマーが10秒(timeInterval: 10.0)の後に発火して、ターゲット・オブジェクト(target: selfによるこの場合は、あなたのビューコントローラ)メソッドを実行するようになりますが(selector: @selector(horror)のため)。あなたはそれをキャンセルする必要がある場合

... 
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval: 10.0 target: self selector: @selector(horror) userInfo: nil repeats: NO]; 
self.myTimerProperty = timer; 
... 

をして:任意のチャンスは、あなたのビューコントローラは、時間がIVARに、安全なタイマーインスタンスを経過する前に非アクティブになり、それを取り消すことができること、がある場合

:あなたはこれをやっている場合

... 
if (self.myTimerProperty) 
{ 
    // Ok. Since we have a timer here, we must assume, that we have set it 
    // up but it did not fire until now. So, cancel it 
    [self.myTimerProperty invalidate]; 
    self.myTimerProperty = nil; 
} 
... 

ところで、それはおそらく良いアイデアは、コールバックメソッド内 からタイマープロパティをクリアします

+0

thanx thats right :) –

関連する問題