选择器'myTimer'没有已知的类方法

时间:2013-02-21 21:49:03

标签: iphone objective-c cocoa-touch ios6

基本上有两个视图控制器oneviewcontroller和mainviewcontroller。在oneviewcontroller中定义了myTimer。在mainviewcontroller中我试图使用UIButton暂停和恢复myTimer。但是获取消息选择器的未知类方法' myTimer'

在OneViewController中将NSTimer定义为myTimer

@property (nonatomic, retain) NSTimer *myTimer;

@synthesize myTimer;

- (void)viewDidLoad {

[self myTimerMethod];

[super viewDidLoad];

}


- (void)myTimerMethod{

 NSLog(@"myTimerMethod is Called");

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

}

- (void)updateView:(NSTimer *)theTimer
 {
 if  (index < [textArray count])
 {

  self.textView.text = [self.textArray objectAtIndex:index];
  self.imageView.image = [self.imagesArray objectAtIndex:index];
  index++;
  }else{

  index = 0;

  }

我试图暂停并从mainviewcontroller恢复myTimer

在mainviewcontroller.h中

   @class OneViewController;

   @property (strong, nonatomic) OneViewController *oneviewcontroller;
   @synthesize oneviewcontroller = _oneviewcontroller;

  -(void)playpauseAction:(id)sender {

   if([audioPlayer isPlaying])
    {
    [sender setImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];

    [audioPlayer pause];

    [[OneViewController myTimer] invalidate];    **\\No Known class method for selector 'myTimer'**



    }else{

    [sender setImage:[UIImage imageNamed:@"audiostop.png"] forState:UIControlStateNormal];

    [audioPlayer play];

    [[OneViewController myTimer] fire];  **\\No Known class method for selector 'myTimer'**


    if(isFirstTime == YES)
    {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:06.0
                                                      target:self
                                                    selector:@selector(displayviewsAction:)
                                                    userInfo:nil
                                                     repeats:NO];
        isFirstTime  = NO; } } }

我该如何修复此错误。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

而不是[[OneViewController myTimer] invalidate];使用[self.oneviewcontroller.myTimer invalidate];

此外,在OneViewController实现中,而不是myTimer = [NSTimer scheduledTimerWithTimeInterval:.....使用self.myTimer = [NSTimer scheduledTimerWithTimeInterval:.....