2010-06-02 77 views
1

在我第一次在三視圖 - 控制調用計時器這樣如何從另一個視圖控制器調用NStimer形式的一個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]; 
} 

首先視圖 - 控制開.. 在第一視圖控制器有一個button.In按鈕操作 我寫了

- (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 
} 

定時器啓動..但視圖沒有打開(第一個)vi ewcontroller .......

請幫我......

+0

我不明白你想用此BVC對象或什麼的那點做什麼方法是。你能進一步解釋嗎? – 2010-06-02 17:59:06

回答

1

巴拉,

放置的NSTimer對象在App代表.h文件中,包括在哪裏.h文件中你使用計時器。這將允許NSTimer成爲一個全局計時器,您可以從包含應用程序委託頭文件的任何其他視圖調用該計時器。

在應用程序委託.h文件中(在適當的地方):

NSTimer *delayTimer; 

@property (nonatomic, retain) NSTimer *delayTimer; 

合成這種在應用程序委託.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]; 

然後,當你想invali從某處日起,你只是這樣做:

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

如果,如果我需要有一個倒計時器應該認爲controllers.do之間共享,我必須讓它在應用程序的委託其中剩餘秒也可以是一個伊娃在應用程序代表財產? 在這種情況下,我會更新剩餘秒

+0

我解決了這個問題,當我第一次啓動定時器時,將定時器的endtimestamp保存爲nsuserdefaults。當viewcontroller加載它的視圖時,它從用戶默認值中讀取endtimesamp並根據剩餘時間啓動它自己的定時器。 – 2012-08-01 10:28:19

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; 
} 
相關問題