Iphone 如何从另一个viewcontroller调用一个viewcontroller中的NStimer?

Iphone 如何从另一个viewcontroller调用一个viewcontroller中的NStimer?,iphone,Iphone,第一次我在第三个viewcontroller中这样调用计时器 timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(targetMethod) userInfo:nil repeats:NO]; 然后计时器调用targetMethod -(void)targetMethod { First * sVC = [[First alloc] initWithNibName:@"First

第一次我在第三个viewcontroller中这样调用计时器

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

请帮帮我

将NSTimer对象放置在App Delegate.h文件中,并将该.h文件包含在使用计时器的任何位置。这将允许NSTimer成为一个全局计时器,您可以从包含应用程序委托头文件的任何其他视图调用它

应用程序内delegate.h文件(在适当的区域中):

在app delegate.m文件中合成此文件(记住在dealloc中发布):

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

// 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;
巴拉

将NSTimer对象放置在App Delegate.h文件中,并将该.h文件包含在使用计时器的任何位置。这将允许NSTimer成为一个全局计时器,您可以从包含应用程序委托头文件的任何其他视图调用它

应用程序内delegate.h文件(在适当的区域中):

在app delegate.m文件中合成此文件(记住在dealloc中发布):

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

// 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;

如果我需要在视图控制器之间共享一个倒计时计时器,该怎么办?我是否必须在应用程序委托中设置倒计时计时器,其中剩余的秒数可以是ivar,也可以是具有属性的应用程序内委托?
在这种情况下,我会在更新剩余的秒数后发布来自计时器处理程序的通知。如果我需要一个倒计时计时器,该计时器应在视图控制器之间共享,该怎么办?我是否必须在应用程序委托中设置它,其中剩余的秒数可以是ivar,也可以是具有属性的应用程序内委托? 在这种情况下,我将在更新AppDelegate.h中剩余的秒数后发布来自计时器处理程序的通知

@property int timePassed;
在AppDelegate.m中

@synthesize timePassed;
在ViewController.h中

@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;
@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;
}
- (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;
}
在SecondViewController.h中

@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;
在SecondViewController.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;
}
- (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;
}
在AppDelegate.h中

@property int timePassed;
在AppDelegate.m中

@synthesize timePassed;
在ViewController.h中

@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;
@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;
}
- (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;
}
在SecondViewController.h中

@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic)NSTimer *timer;
@property (weak, nonatomic) IBOutlet UILabel *time;
在SecondViewController.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;
}
- (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;
}

我不明白你想用这个BVC对象做什么,或者这个方法的意义是什么。你能进一步解释吗?我不明白你想用这个BVC对象做什么,或者这个方法的意义是什么。你能进一步解释一下吗?我通过在第一次启动计时器时将计时器的endtimestamp保存为nsuserdefaults解决了这个问题。当viewcontroller加载其视图时,它会从用户默认值读取endtimestamp并根据剩余时间启动自己的计时器。我通过将计时器的endtimestamp保存为nsuserdefaults解决了这个问题第一次启动计时器时nsuserdefaults。当viewcontroller加载其视图时,它从用户默认值中读取endtimesamp,并根据剩余时间启动自己的计时器。