2012-07-07 20 views
0

我的應用程序已經在視圖2 3次如何坡平到視圖2

在廠景只有一個按鍵(播放1級)

是推動以veiw2和更新titlelabel

視圖2負載時

,並出現一個定時器時重啓定時器開始倒計時,並更新視圖2本身的另一個標籤,也視圖2具有按鈕暫停計時器並推向VIEW3

在VIEW3只有一個按鈕,彈出回視圖2和恢復你定時器

我的問題是,當我流行f從view3到view2定時器仍然暫停

請幫忙嗎?請致電&親切的問候。

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 

回答

1

這是因爲在view3back方法,您創建的view2obj新的即時和設置paused=NO。但是,當您彈出時,objview2不同。您需要做的是使用委託方法模式,以便view3將在view2中使用回調方法將paused設置爲NO

有關委託方法模式的示例,請查看SO中的示例。

編輯:示例回調代碼:

使這些添加/修改了代碼:

在view2.h:

#import "view3.h" 

@interface view2 : UIViewController <view3Delegate> 

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

In 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:

@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]; 

} 
+0

其實我是新來的Xcode,對不起,但不能完全得到你的答案。你能解釋一下嗎?我以某種方式理解,obj不是view2的同一時間點,但不能創建一個委託方法,如您放置鏈接的示例所示。 – 2012-07-07 18:16:35

+0

請參閱示例代碼 – user523234 2012-07-07 19:03:44

+0

非常感謝,您的代碼對我非常有幫助,實際上它解決了我的問題。 – 2012-07-07 19:32:11