我如何从另一个Viewcontroller调用NStimer形成一个Viewcontroller? [英] How can i call NStimer form one viewcontroller from unother viewcontroller?
问题描述
第一次,我在Third Viewcontroller中这样调用计时器
At first time i call the timer like this in Third viewcontroller
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(targetMethod) userInfo:nil repeats:NO];
然后将计时器称为targetMethod
Then timer called the targetMethod
-(void)targetMethod
{
First * sVC = [[First alloc] initWithNibName:@"First" bundle:[NSBundle mainBundle]];
[self presentModalViewController:sVC animated:YES];
[sVC release];
[timer invalidate];
}
第一个viewcontroller打开.在第一个视图控制器中有一个按钮.在按钮动作中我写了
First viewcontroller opened.. In First viewcontroller had one button.In button action i wrote
- (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 .......
timer started..But view didn't open (first )viewcontroller.......
请帮助我......
Please help me......
推荐答案
巴拉,
将NSTimer对象放置在App Delegate .h文件中,并在使用计时器的任何位置包括该.h文件.这将使NSTimer成为全局计时器,您可以从任何其他包含应用程序委托头文件的视图中调用它.
Place the NSTimer object in the App Delegate .h file, include that .h file in wherever you use the timer. This will allow the NSTimer to be a global timer which you can call from any other view that includes the app delegate header file.
在应用程序委托.h文件中(在适当的区域中):
In app delegate .h file (in the appropriate areas):
NSTimer *delayTimer;
@property (nonatomic, retain) NSTimer *delayTimer;
在应用程序委托.m文件中合成该文件(记住要在dealloc中释放它):
Synthesize this in the app delegate .m file (remember to release it in dealloc):
@synthesize delayTimer;
然后在您使用计时器的任何视图中,都可以像这样访问它:
Then in whichever views you use the timer in, access it like this:
// get global variable
SomeNameHereAppDelegate *mainDelegate = (SomeNameHereAppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.delayTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(targetMethod) userInfo:nil repeats:NO];
然后,当您想从某处使它失效时,只需执行以下操作:
Then when you want to invalidate it from somewhere, you just do this:
[mainDelegate.delayTimer invalidate];
mainDelegate.delayTimer = nil;
这篇关于我如何从另一个Viewcontroller调用NStimer形成一个Viewcontroller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!