我是 iPhone 开发的新手,正在使用导航控制器和情节提要制作一个倒计时项目。我的应用程序有两个视图。第一个视图只有一个按钮。单击此按钮时,它将转到第二个视图。第二个视图有倒计时对象。我的问题是,当倒计时在第二个视图中运行时,如果我返回第一个视图然后点击按钮转到第二个视图,则倒计时不再运行。
这是代码:
视图1.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
视图2.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