本文介绍了弹出到view2时如何恢复计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用有 3 次浏览

my application has 3 views

在视图 1 中只有一个按钮(播放级别 1)推送到 veiw2 并更新视图 2 中的标题标签

in view1 only one button (play level 1) that pushes to veiw2 and updates titlelabel in view2

当 view2 加载并出现计时器开始倒计时并更新 view2 本身中的另一个标签时,view2 也有按钮暂停计时器并推送到 view3

when view2 loads and appears a timer starts to count down and update another label in view2 itself, also view2 has button that pauses the timer and pushes to view3

在 view3 中只有一个按钮可以弹回 view2 并恢复计时器

in view3 only one button that pop back to view2 and resumes thee timer

我的问题是,当我从 view3 弹出到 view2 时,计时器仍然暂停

my problem is that when i pop from view3 to view2 the timer is still paused

有什么帮助吗?

谢谢&亲切的问候.

Thanks & kind regards.

view1.h

#import <UIKit/UIKit.h>

@interface view1 : UIViewController





-(IBAction)nextpage;

@end

view1.m

#import "view1.h"
#import "view2.h"
@interface view1 ()

@end

@implementation view1




-(IBAction)nextpage{

    view2 *obj = [[view2 alloc] init];
    [self.navigationController pushViewController:obj animated:YES];
    [obj.leveltitle setText:@"Level"];
    obj.time=10;

}




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

view2.h

#import <UIKit/UIKit.h>

@interface view2 : UIViewController



{
    int time;
    NSTimer *timer;
    BOOL paused;
}

@property (nonatomic, retain) NSTimer *timer;
@property (readwrite, nonatomic) int time;
@property (nonatomic) BOOL paused;


@property (readwrite, retain) IBOutlet UILabel *leveltitle;
@property (nonatomic, readwrite) IBOutlet UILabel *label;



-(IBAction)back;
-(IBAction)pause;


@end

view2.m

#import "view2.h"
#import "view3.h"
@interface view2 ()

@end

@implementation view2

@synthesize leveltitle, label;
@synthesize time, timer;
@synthesize paused;

-(void)countDown2
{
    if (paused == NO && time>0)
 {
        time = time -1 ;
     view3 *obj = [[view3 alloc] init];
     obj.sss = time;
    if (time == 0)
 {
            [timer invalidate];

 }

        [label setText:[NSString stringWithFormat:@"%i",time]];
 }


}


-(IBAction)back{

    [self.navigationController popViewControllerAnimated:YES];

}


-(IBAction)pause{

     view3 *obj = [[view3 alloc] init];
    [self.navigationController pushViewController:obj animated:YES];
     paused = YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}



- (void)viewDidLoad
{
    paused = NO;
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self     selector:@selector(countDown2) userInfo:nil repeats:YES];



    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:    (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

view3.h

#import <UIKit/UIKit.h>

@interface view3 : UIViewController

{

    int sss;
}

@property (nonatomic) int sss;
-(IBAction)back;


@end

view3.m

#import "view3.h"
#import "view2.h"
@interface view3 ()

@end

@implementation view3
@synthesize sss;

-(IBAction)back{

    view2 *obj = [[view2 alloc] init];
    obj.paused=NO;
    obj.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self     selector:@selector(cont) userInfo:nil repeats:YES];
     //[obj.timer fire];
    [self.navigationController popViewControllerAnimated:YES];

}



 -(void)cont{
     view2 *obj = [[view2 alloc] init];
     obj.time = sss;
     if (obj.paused == NO && time>0)
     {
         obj.time = obj.time -1 ;

         if (obj.time == 0)
         {
             [obj.timer invalidate];

         }

         [obj.label setText:[NSString stringWithFormat:@"%i",obj.time]];
     }



 }


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

推荐答案

这是因为在view3back方法中,你创建了一个新的瞬间view2 命名 obj 并设置 paused=NO.但是当你弹回来时,那个 objview2 是不一样的.你需要做的是使用委托方法模式,这样view3就会在view2中有一个回调方法,将paused设置为.

It is because in view3's back method, you created a new instant of view2 name obj and set paused=NO. But that obj was not the same instant of view2 when you pop back. What you need to do is to use a delegate method pattern, so that view3 will have a callback method in view2 to set the paused to NO.

有关委托方法模式的示例,请查看以下示例 SO.

For an example of delegate method pattern, check out an example from this SO.

示例回调代码:

对您的代码进行这些添加/更改:

Make these addition/changes to your code:

在view2.h中:

#import "view3.h"

@interface view2 : UIViewController <view3Delegate>

在 view2.m 中:

In view2.m:

-(IBAction)pause{

    view3 *obj = [[view3 alloc] init];
    obj.delegate = self;  //adding this line
    [self.navigationController pushViewController:obj animated:YES];
    paused = YES;
}

-(void)setPausedValue:(BOOL)value   //this is the new method callback from view3 to set paused value
{
    paused = value;
}

在view3.h中:

#import <UIKit/UIKit.h>
@protocol view3Delegate <NSObject>
-(void) setPausedValue:(BOOL)value;
@end
@interface view3 : UIViewController
{
    int sss;
}
@property (assign) id <view3Delegate> delegate;
@property (nonatomic) int sss;
-(IBAction)back;
@end

在 view3.m 中:

In view3.m:

@implementation view3
@synthesize delegate;

-(IBAction)back{

//    view2 *obj = [[view2 alloc] init];
//    obj.paused=NO;
    [self.delegate setPausedValue:NO];  //make callback and set value
    obj.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self     selector:@selector(cont) userInfo:nil repeats:YES];
    //[obj.timer fire];
    [self.navigationController popViewControllerAnimated:YES];

}

这篇关于弹出到view2时如何恢复计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 13:25