my application has 3 views
in view1 only one button (play level 1) that pushes to veiw2 and updates titlelabel in view2
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
in view3 only one button that pop back to view2 and resumes thee timer
my problem is that when i pop from view3 to view2 the timer is still paused
any help please?
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