0

myTimer 在 OneViewController 中定义,我试图从 mainviewcontroller 使其无效并再次触发它,但它不起作用。我到底在这里缺少什么。

这是我的代码

OneViewController.h

@property (nonatomic, strong) NSTimer *myTimer;

OneViewController.m

@synthesize myTimer;

- (void)viewDidLoad
{

 [super viewDidLoad];


[self myTimerMethod];}

- (void)myTimerMethod{

 NSLog(@"myTimerMethod is Called");

self.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.h

 @class OneViewController;

 @property (strong, nonatomic) OneViewController *oneviewcontroller;


 MainViewController.m

  @synthesize oneviewcontroller = _oneviewcontroller;

-(void)playpauseAction:(id)sender {

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

    [audioPlayer pause];

    [self.oneviewcontroller.myTimer invalidate];

}else{

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

    [audioPlayer play];


    [self.oneviewcontroller.myTimer fire];



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


  - (void)displayviewsAction:(id)sender{

OneViewController *oneviewcontroller = [[OneViewController alloc] init];

oneviewcontroller.view.frame = CGRectMake(0, 0, 320, 430);

CATransition *transitionAnimation = [CATransition animation];

[transitionAnimation setDuration:1];

[transitionAnimation setType:kCATransitionFade];

[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];

[self.view addSubview:oneviewcontroller.view];

 }

感谢帮助。

谢谢。

4

1 回答 1

2

您将 OneViewController 创建为方法 displayviewsAction 的局部变量。不像您之前定义的属性。

这就是它不起作用的原因,一旦退出方法,变量就会被释放。

更改此行:

OneViewController *oneviewcontroller = [[OneViewController alloc] init];

有了这个:

self.oneviewcontroller = [[OneViewController alloc] init];
于 2013-02-23T18:58:16.877 回答