我是一個新的程序員(我自己學習objective-c)。我試圖製作一個iPad應用程序,它將使用倒數計時器作爲主視圖,然後根據我從數據庫中提取的不同數據,使用警報繼續播放到不同的視圖中。我已經做了幾天的工作,並且我不能讓這個計時器停在0,並且發出警報,讓用戶進入下一個視圖。它啓用了ARC。IOS停止倒數計時器在0
下面是該.H代碼:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
int eventTime;
NSDate *eventDate;
IBOutlet UILabel *timerLabel;
NSTimer *timer;
}
-(void)updateCountDown;
-(void)exercisePopup;
@end
而對於.M:
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountDown) userInfo:nil repeats:YES];
if(![timerLabel isEqual: 0]) {
[timer invalidate];
[self exercisePopup];
}
}
- (void)updateCountDown {
eventTime = 10;
eventDate = [NSDate dateWithTimeIntervalSinceNow:eventTime];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
int units = NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate: eventDate options:0];
[timerLabel setText:[NSString stringWithFormat:@"%ld%c:%ld%c:%ld%c", (long)[components hour], 'h', (long)[components minute], 'm', (long)[components second], 's']];
}
-(void)exercisePopup{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Time to Exercise" message:@"It's Time to Exercise, Touch the Screen" delegate:self cancelButtonTitle:@"Touch to Exercise" otherButtonTitles:nil];
// optional - add more buttons:
[alert show];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我有越來越定時器0停止,使警報的問題會着火。如果我離開了!在if語句中,當應用程序立即打開警報時觸發,但倒計時從不開始。如果我把它拿出來,倒數會永遠變成負數等等。我不知道該如何檢查if語句,因爲它似乎認爲timerLabel從0開始。我也試着移動if語句,甚至使它成爲自己的方法,然後嘗試在計時器加載,但沒有我試過正在工作。
完全迷路了,我一直在嘗試所有的解決方案,我能找到這裏的最後幾天和他們沒有什麼,我試圖做的工作,我已經得到了各種我實施它們時出現錯誤。如果有人有任何建議,我會很感激。
謝謝!這工作完美。我也使用伊恩麥克唐納的答案來構建應用程序的其餘部分。非常感謝大家幫助我解決這個問題! – user2730433 2014-11-07 16:55:43