2014-11-05 151 views
0

我是一個新的程序員(我自己學習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語句,甚至使它成爲自己的方法,然後嘗試在計時器加載,但沒有我試過正在工作。

完全迷路了,我一直在嘗試所有的解決方案,我能找到這裏的最後幾天和他們沒有什麼,我試圖做的工作,我已經得到了各種我實施它們時出現錯誤。如果有人有任何建議,我會很感激。

回答

0

我把這段代碼放在一個示例項目中,它工作。搏一搏。我對你想要做的事情做了一些小的改變。你正在做一些事情不正確。在啓動計時器後立即調用的viewDidLoadMethod過程中,您檢查計時器是否爲0的if語句會被調用。

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UILabel *timerLabel; 
@property (nonatomic, strong) NSTimer *timer; 
@property (nonatomic) NSInteger currentTime; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.currentTime = 10; 
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; 
} 

- (void)updateTimer 
{ 
    NSDate *eventDate = [NSDate dateWithTimeIntervalSinceNow:self.currentTime]; 

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 

    int units = NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond; 

    NSDateComponents *components = [calendar components:units fromDate:[NSDate date] toDate: eventDate options:0]; 

    [self.timerLabel setText:[NSString stringWithFormat:@"%ld%c:%ld%c:%ld%c", (long)[components hour], 'h', (long)[components minute], 'm', (long)[components second], 's']]; 
    self.currentTime--; 
    if (self.currentTime == 0) 
    { 
     [self.timer invalidate]; 
     [self exercisePopup]; 
    } 

} 
-(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]; 
} 


@end 
+0

謝謝!這工作完美。我也使用伊恩麥克唐納的答案來構建應用程序的其餘部分。非常感謝大家幫助我解決這個問題! – user2730433 2014-11-07 16:55:43

0

if(![timerLabel isEqual: 0]

這條線是你的問題。您將UILabel與0進行比較,這與檢查此UILabel是否爲零相同,它不在viewDidLoad中,因爲您從存在此標籤的nib文件進行加載。因此[timerLabel isEqual:0]始終等於false,這就是否定整個if語句使警報觸發的原因。你需要考慮一個計數10次重複的射擊的替代方法。

請嘗試保留updateCountDown已使用該方法中聲明的static int發起了多少次計數。

0

看來你在這裏誤解了一些東西。

程序流

當你實例化你的計時器,它熄滅自身,然後調用您的選擇每1.0秒。原始執行繼續正常,檢查您的條件,然後退出viewDidLoad。你永遠不會再檢查條件。這是你的事件的基本排序:

  1. 您創建一個定時器。
  2. 您檢查,看看是否timerLabel0
  3. 一秒鐘後,你的計時器觸發,執行updateCountDown

exercisePopup方法不會被調用,因爲你不updateCountDown檢查你的病情。

對象比較

你使用timerLabelisEqual:和整數文字0是一樣的,如果你寫的比較:

if (![timerLabel isEqual:nil]) { 

這顯然不是你所預期的;您正試圖將與timerLabel關聯的字符串值與字符串值@"0"進行比較。

if (![timerLabel.text isEqualToString:@"0"]) { 

但是,這不是比較時間的最佳方法。您應該考慮使用NSTimeInterval並對其進行比較。您也可能想要使用CADisplayLink來查看您的顯示器的時間更新安排,以便您不會等待可能不同步的定時器。