tags:

views:

81

answers:

2

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

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];
}

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
}

timer started..But view didn't open (first )viewcontroller.......

Please help me......

A: 

Bala,

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.

In app delegate .h file (in the appropriate areas):

NSTimer *delayTimer;

@property (nonatomic, retain) NSTimer *delayTimer;

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;
iWasRobbed
A: 

Hi Rob, i used like this.First time it works fine.First viewcontroller opened using this coding.

First * sVC = [[First alloc] initWithNibName:@"First" bundle:[NSBundle mainBundle]]; [self presentModalViewController:sVC animated:YES];

In this viewcontroller having one button. On that Button i close the First viewcontroller and call the timer like this.

  • (IBAction) Action:(id)sender { [self dismissModalViewControllerAnimated:YES]; SomeNameHereAppDelegate *Delegate = (SomeNameHereAppDelegate *)[[UIApplication sharedApplication] delegate]; Third *BVC=[[Third alloc]init]; Delegate.Timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:BVC selector:@selector(targetMethod) userInfo:nil repeats:Yes]; }

Timer called the target function in thirdviewcontroller but it didn't open the First viewcontroller..

This is my problem

Bala