私は iPhone 開発の初心者で、ナビゲーション コントローラーとストーリーボードを使用してカウントダウン プロジェクトを作成しています。私のアプリケーションには 2 つのビューがあります。最初のビューにはボタンが 1 つしかありません。このボタンをクリックすると、2 番目のビューに移動します。2 番目のビューにはカウントダウン オブジェクトがあります。私の問題は、カウントダウンが 2 番目のビューで実行されているときに、最初のビューに戻ってボタンを押して 2 番目のビューに移動すると、カウントダウンが実行されなくなることです。
コードは次のとおりです。
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