简体   繁体   English

无法暂停并在另一节课中恢复nstimer

[英]Cant pause and resume nstimer in another class

Have myTimer defined in class One 在类1中定义了myTimer

 @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. 我想在mainviewcontroller类中暂停并恢复myTimer。

In mainviewcontroller.h 在mainviewcontroller.h中

@property (nonatomic, strong) ClassOne *oneclass;

and in mainviewcontroller.m 并在mainviewcontroller.m中

@synthesize oneclass;

then via UIButton in mainviewcontroller pausing and resuming myTimer 然后通过mainviewcontroller中的UIButton暂停并恢复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 但是出现语义问题和ARC语义问题错误

unknown receiver myTimer did you mean NSTimer. 未知接收者myTimer您的意思是NSTimer。 No known class method for selector invalidate and fire 没有已知的选择器失效和触发类方法

Thanks for help. 感谢帮助。

Is myTimer set as a property of ClassOne ? 是否将myTimer设置为ClassOne的属性?

@property (nonatomic, strong) NSTimer *myTimer;

and

@synthesize myTimer;

Then you can call [oneClass.myTimer invalidate]; 然后,您可以调用[oneClass.myTimer invalidate]; and [oneClass.myTimer fire]; [oneClass.myTimer fire]; in your mainviewcontroller. 在您的mainviewcontroller中。

It looks like you are creating the timer object in the viewDidLoad method call. 看来您正在viewDidLoad方法调用中创建计时器对象。 Make sure the view is actually loaded before you try to manipulate the timer property or the results will not be pleasant. 在尝试操作timer属性之前,请确保已实际加载视图,否则结果将不令人满意。 Also, like @Anoop Vaidya said, you need to make sure you initialize a class One object before attempting to access its properties. 同样,就像@Anoop Vaidya所说的那样,您需要确保在尝试访问类的属性之前初始化类One对象。 A good way to do this is to modify your mainViewController code in the following way: 做到这一点的一种好方法是通过以下方式修改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;
     }
   } 
}

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. 或者,您可以从第二类向第一类发送通知,该通知将被观察,然后将调用您所需的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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