3

我是 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
4

2 回答 2

0

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

为此,您必须以某种方式使计时器保持活动状态。您可以修改您的应用程序委托来管理计时器,并View2从委托中获取其计时器信息(例如)。

于 2012-10-30T21:40:38.600 回答
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 为,

@interface view1 : UIViewController
{

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

每次进入下一页时,您都在创建一个新view2的 ,您需要对其进行更改。

于 2012-10-30T21:44:26.127 回答