如何从另一个viewcontroller调用NStimer从一个viewcontroller调用?

时间:2010-06-02 16:32:00

标签: iphone

我第一次在第三个视图控制器中调用这样的计时器

timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(targetMethod) userInfo:nil repeats:NO];

然后计时器调用targetMethod

-(void)targetMethod
{
 First * sVC = [[First alloc] initWithNibName:@"First" bundle:[NSBundle mainBundle]];
 [self presentModalViewController:sVC animated:YES];
 [sVC release];
 [timer invalidate];
}

第一个viewcontroller打开.. 在第一个viewcontroller中有一个按钮。在按钮动作中 我写了

- (IBAction) Action:(id)sender
{
 [self dismissModalViewControllerAnimated:YES]; 
 Third *BVC=[[Third alloc]init];
    [Bvc TimerStart];       //Timestart is function i start timer in this function..
 //i want to call Third  viewcontroller timer function this place
}

计时器启动..但是视图没有打开(第一个)viewcontroller .......

请帮帮我......

3 个答案:

答案 0 :(得分:1)

巴拉,

将NSTimer对象放在App Delegate .h文件中,在您使用计时器的任何地方包含该.h文件。这将允许NSTimer成为全局计时器,您可以从包含app委托头文件的任何其他视图调用该计时器。

在app delegate .h文件中(在适当的区域):

NSTimer *delayTimer;

@property (nonatomic, retain) NSTimer *delayTimer;

在app delegate .m文件中合成它(记得在dealloc中释放它):

@synthesize delayTimer;

然后,在您使用计时器的任何视图中,访问它如下:

// get global variable
SomeNameHereAppDelegate *mainDelegate = (SomeNameHereAppDelegate *)[[UIApplication sharedApplication] delegate];    

mainDelegate.delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(targetMethod) userInfo:nil repeats:NO];

然后当你想从某个地方使它失效时,你就这样做了:

[mainDelegate.delayTimer invalidate];
 mainDelegate.delayTimer = nil;

答案 1 :(得分:0)

如果我需要一个应该在视图控制器之间共享的倒计时器怎么办。如果我必须在app委托中进行,其余的秒也可以是带有属性的app委托中的ivar? 在这种情况下,我会在更新剩余秒数后发布计时器处理程序的通知

答案 2 :(得分:0)

在AppDelegate.h中

@property int timePassed;

在AppDelegate.m中

@synthesize timePassed;

在ViewController.h中

@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;

在ViewController.m中

- (void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];
AppDelegate *delegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
delegate.timePassed = 180;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(TimePasses) userInfo:nil repeats:YES];;
}


-(void) TimePasses{
AppDelegate *delegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
delegate.timePassed = delegate.timePassed - 1;
NSLog(@"TimePasses %d", delegate.timePassed);
int minuts = delegate.timePassed / 60;
int seconds = delegate.timePassed - (minuts * 60);

NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minuts, seconds];
time.text = timerOutput;
}

在SecondViewController.h中

@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;

在SecondViewController.m中

- (void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(TimePasses) userInfo:nil repeats:YES];;
}

-(void) TimePasses{
AppDelegate *delegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
NSLog(@"TimePasses in 2 %d", delegate.timePassed);
int minuts = delegate.timePassed / 60;
int seconds = delegate.timePassed - (minuts * 60);

NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minuts, seconds];
time.text = timerOutput;
}