2013-02-20 63 views
0

我有類one.m和類one.m定義的NSTimer訪問的NSTimer從一類到另一個類

myTimer = [NSTimer scheduledTimerWithTimeInterval:2.6 
           target:self 
           selector:@selector(updateView:) 
           userInfo:nil 
           repeats:YES]; 

我怎樣才能在mainviewcontroller訪問此的NSTimer暫停和恢復它。

回答

0

您可以使計時器成爲一個屬性,然後從MainViewController類訪問它。 就像這樣

@interface One: UIDelegate { 
    //variables 
} 
@property(nonatomic, retain) NSTimer* myTimer; 

@implementation One 
@synthesize myTimer; 
//other 

@implementation MainViewController 
One *one = [[One alloc]init]; 
one.myTimer; //now you can access the timer 
+0

這就是我在做的,但它是說未知的接收器myTimer你的意思是NSTimer。沒有已知的類選擇器無效方法 – user1452248 2013-02-20 19:59:35

+0

您不想調用NSTimer,因爲您尚未指定要停止的計時器。嘗試通過調試器運行它,看看你的'OneTimer'類是否與'MainViewController'中的'one.myTimer'相同 – Jeeter 2013-02-20 20:02:20