弹出到根视图时恢复倒计时

时间:2012-10-30 21:28:22

标签: ios xcode nstimer

我是iPhone开发新手,正在使用导航控制器和故事板制作倒计时项目。我的申请有两个观点。第一个视图只有一个按钮。单击此按钮时,将转到第二个视图。第二个视图有倒计时对象。我的问题是当倒计时在第二个视图中运行时,如果我返回第一个视图然后点击按钮转到第二个视图,则倒计时不再运行。

以下是代码:

view1.h

#import <UIKit/UIKit.h>
#import "view2.h"

@interface view1: UIViewController

-(IBAction)nextpage:(id)sender;

@end

view1.m

@implementation view1


-(IBAction)nextpage:(id)sender
{
    view2 *next=[self.storyboard instantiateViewControllerWithIdentifier:@"secondview"];
    [self.navigationController pushViewController:next animated:YES];
}

@end

view2.h

@interface view2 : UIViewController
{
    IBOutlet UILabel *lbl;
    IBOutlet UITextField *field;

    NSTimer *theTimer;
    NSDate *targetDate;
    NSCalendar *cal;
    NSDateComponents *components;
}

@property (nonatomic,retain) IBOutlet UILabel *lbl;
@property (nonatomic,retain) IBOutlet UITextField *field;
-(IBAction)back_first_view;
@end

view2.m

@implementation view2
@synthesize lbl,field;

- (void)viewDidLoad
{
    cal = [[NSCalendar currentCalendar] retain];
    components = [[NSDateComponents alloc] init];
}

- (IBAction)buttonPressed:(id)sender {

    if (theTimer != nil) {
        return;
    }

    NSString *input = field.text;
    NSArray *timeSplit = [input componentsSeparatedByString:@":"];
    NSUInteger hours =  [[timeSplit objectAtIndex:0] intValue];
    NSUInteger minutes =  [[timeSplit objectAtIndex:1] intValue];
    NSDate *now = [NSDate date];
    NSDateComponents *dateComponents = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now];
    [dateComponents setHour:hours];
    [dateComponents setMinute:minutes];

    if (!targetDate) {
        targetDate = [[cal dateFromComponents:dateComponents] retain];
    }
    else {
        targetDate = nil;
        targetDate = [[cal dateFromComponents:dateComponents] retain];
    }

    if ([targetDate timeIntervalSinceNow] > 0) {
        theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(tick) userInfo:nil repeats:YES];
        [self hideKeyboard];
    }
    else {
        targetDate = nil;
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Cannot countdown because time is before now" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)tick {
    if ([targetDate timeIntervalSinceNow] <= 0) {
        //Checks if the countdown completed

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Countdown Completed" message:@"YAY! The countdown has complete" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

        return;
    }
    components = [cal components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date] toDate:targetDate options:0];

    NSInteger hours = [components hour];
    NSInteger minutes = [components minute];
    NSInteger seconds = [components second];

    NSString *output = [NSString stringWithFormat:@"%i Hours\n%i Minutes\n%i Seconds\n", hours, minutes, seconds];
    lbl.text = output;
}

- (void)hideKeyboard {
    if ([field isFirstResponder]) [field resignFirstResponder];
}

- (IBAction)back_first_view
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
 @end

2 个答案:

答案 0 :(得分:0)

由于您使用的是UINavigationController,当您从View2导航回View1时,View2实例(以及在其上运行的计时器)将被销毁。当您第二次导航到View2时,它是一个全新的实例。

为了让你的行为像你期望的那样,你必须以某种方式让计时器保持活力。您可以修改应用程序委托以管理计时器,并让View2从委托中获取其计时器信息(例如)。

答案 1 :(得分:0)

将此方法更改为,

- (IBAction)nextpage:(id)sender
{
    [self.navigationController pushViewController:self.next animated:YES];
}

将第一行添加到viewDidLoad或任何此类方法

- (void)viewDidLoad {
       [super viewDidLoad]; //Always keep super call as first call in a method
       self.next = [self.storyboard instantiateViewControllerWithIdentifier:@"secondview"];
    // Do any additional setup after loading the view.
}

然后在.h文件中声明@interface as,

@interface view1 : UIViewController
{

}
@property (nonatomic, strong) view2 *next;

每次以及每次进入下一页时,您都要创建一个新的view2,您需要更改它。