简体   繁体   中英

Cant pause and resume nstimer in another class

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.

Is myTimer set as a property of ClassOne ?

@property (nonatomic, strong) NSTimer *myTimer;

and

@synthesize myTimer;

Then you can call [oneClass.myTimer invalidate]; and [oneClass.myTimer fire]; in your mainviewcontroller.

It looks like you are creating the timer object in the viewDidLoad method call. Make sure the view is actually loaded before you try to manipulate the timer property or the results will not be pleasant. Also, like @Anoop Vaidya said, you need to make sure you initialize a class One object before attempting to access its properties. A good way to do this is to modify your mainViewController code in the following way:

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

As you are calling timer from other class, you need to call by creating an object of first class.

Or you can send a notification to first class from second class, that notification will be observed and then your required method will be called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM