-1

Have myTimer defined in class One

 @property (nonatomic, retain) NSTimer *myTimer;

 @synthesize myTimer;

- (void)viewDidLoad {

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

[super viewDidLoad];

}

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

}

I want to pause and resume myTimer in mainviewcontroller class.

In mainviewcontroller.h

@property (nonatomic, strong) ClassOne *oneclass;

and in mainviewcontroller.m

@synthesize oneclass;

then via UIButton in mainviewcontroller pausing and resuming myTimer

-(void)playpauseAction:(id)sender {

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

    [audioPlayer pause];

    [myTimer invalidate];


}else{

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

    [audioPlayer play];

    [myTimer fire];

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

But getting semantic issue and ARC Semantic issue error

unknown receiver myTimer did you mean NSTimer. No known class method for selector invalidate and fire

Thanks for help.

4

3 回答 3

1

看起来您正在 viewDidLoad 方法调用中创建计时器对象。在您尝试操作计时器属性之前,请确保实际加载了视图,否则结果将不会令人愉快。此外,就像@Anoop Vaidya 所说,您需要确保在尝试访问其属性之前初始化一个类 One 对象。这样做的一个好方法是通过以下方式修改您的 mainViewController 代码:

-(void)playpauseAction:(id)sender {
   //This code assumes there is an ivar in mainViewController called aClassOneObject
   if([audioPlayer isPlaying]) {
     [sender setImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];
     [audioPlayer pause];

     if (aClassOneObject)
       [[aClassOneObject myTimer] invalidate];
     else
       //Log error that the aClassOneObject was nil

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

     [audioPlayer play];

     if (aClassOneObject)
       [[aClassOneObject myTimer] fire];
     else
       //Log error that the aClassOneObject was nil

     if(isFirstTime == YES) {
       self.timer = [NSTimer scheduledTimerWithTimeInterval:06.0
                                                     target:self
                                                   selector:@selector(displayviewsAction:)
                                                   userInfo:nil
                                                    repeats:NO];
       isFirstTime  = NO;
     }
   } 
}
于 2013-02-21T17:20:50.303 回答
1

myTimer 是否设置为 的属性ClassOne

@property (nonatomic, strong) NSTimer *myTimer;

@synthesize myTimer;

然后你可以在你的主视图控制器中调用[oneClass.myTimer invalidate];[oneClass.myTimer fire];

于 2013-02-21T17:03:42.653 回答
0

当您从其他类调用计时器时,您需要通过创建第一类的对象来调用。

或者您可以从二等舱向一等舱发送通知,该通知将被观察到,然后您需要的方法将被调用。

于 2013-02-21T17:05:27.933 回答