2014-07-06 85 views
0

我正在嘗試設置倒數計時器。我有兩個視圖控制器:ViewController和EditViewController。 ViewController有兩個小時和分鐘的標籤。 EditViewController有一個UIDatePicker和一個保存按鈕。當用戶在UIDatePicker上選擇一個時間並點擊保存時,它們將被重定向回ViewController,並且時間和分鐘標籤將填充所選時間和當前時間之間的差異。這是自動更新,直到差異爲0.我有問題試圖讓小時和分鐘正確顯示在ViewController標籤上。這裏是我的代碼:倒數計時器:更新控制器視圖標籤

ViewController.m

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.hourLabel.text = self.hourText; 
    self.minuteLabel.text = self.minuteText; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

EditViewController.h

@interface EditViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker; 
@property (weak, nonatomic) IBOutlet NSString *currentTimeString; 
@property (weak, nonatomic) IBOutlet NSString *endTimeString; 
- (IBAction)saveButton:(id)sender; 
- (IBAction)cancelButton:(id)sender; 

-(void)updateTime; 

@end 

EditViewController.m

#import "EditViewController.h" 
#import "ViewController.h" 

@interface EditViewController() 

@end 

@implementation EditViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (IBAction)saveButton:(id)sender 
{ 

    NSCalendar *now = [NSCalendar autoupdatingCurrentCalendar]; 
    NSInteger currentTime = (NSHourCalendarUnit | NSSecondCalendarUnit); 
    self.datePicker.date = [now dateFromComponents:[now components:currentTime fromDate:self.datePicker.date]]; 

    NSTimer *timer; 
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

-(void)updateTime 
{ 
    NSInteger timeLeft = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]); 
    NSInteger minutes = (timeLeft/60) % 60; 
    NSInteger hours = (timeLeft/3600) % 24; 

    ViewController *controller; 
    controller.hourText = [NSString stringWithFormat:@"%ldi",(long)hours]; 
    controller.minuteText = [NSString stringWithFormat:@"%ldi",(long)minutes]; 
    NSLog(@"The time is %ld",(long)timeLeft); 

} 

- (IBAction)cancelButton:(id)sender 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 

任何幫助表示讚賞。提前致謝。

回答

0

查看您的代碼中編輯的方法。這將充分滿足您的要求。

#import "EditViewController.h" 
#import "ViewController.h" 
#import "AppDelegate.h" 
@interface EditViewController() 
{ 
    AppDelegate *appDelegate; 
} 
@end 

@implementation EditViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     // Custom initialization 
     appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    } 
    return self; 
} 


-(void)updateTime 
{ 
    NSInteger timeLeft = ((NSInteger)[self.datePicker.date timeIntervalSinceNow]); 
    NSInteger minutes = (timeLeft/60) % 60; 
    NSInteger hours = (timeLeft/3600) % 24; 

    NSString *hourText = [NSString stringWithFormat:@"%ldi",(long)hours]; 
    NSString *minuteText = [NSString stringWithFormat:@"%ldi",(long)minutes]; 

    [appDelegate updateTime:hourText andMinute:minuteText]; 

} 

最後,定義AppDelegate類的方法

- (void) updateTime:(NSString *)hourStr andMinute:(NSString *)minuteStr 
{ 
    for(id obj in [self.navigationController viewControllers]) 
    { 
     if([obj isKindOfClass:ViewController]) 
     { 
     ViewController *vcObj = (ViewController *)obj; 
     vcObj.hourLabel.text = hourStr; 
     vcObj.minuteLabel.text = minuteStr; 

     } 
    } 
} 
0

幾個問題:

ViewController.m

self.hourLabel.text = self.hourText; 
self.minuteLabel.text = self.minuteText; 

應在viewWillAppear而不是viewDidLoad。發生什麼事是您在應用程序加載時設置文本。 viewDidLoad只會針對該應用的實例觸發一次。你需要做的是在你從EditViewController返回時更新它,對嗎?所以如果你把它放在viewWillAppear中,當你回來並且爲你更新時它也會被調用。

接下來的事情:

EditViewController.m

ViewController *controller; 
controller.hourText = [NSString stringWithFormat:@"%ldi",(long)hours]; 
controller.minuteText = [NSString stringWithFormat:@"%ldi",(long)minutes]; 
NSLog(@"The time is %ld",(long)timeLeft); 

這段代碼被宣告一個新的視圖控制器的變量(當然,大部分的它,你就錯過了= [ViewController new]部分)。這個問題是你不用這個控制器做任何事情。 ViewController實際上已經在您的導航堆棧中(我假設NavigationController是因爲您在用戶返回時說的),所以您需要使用對您的應用已經實例化的ViewController的引用,而不是創建一個新的,因爲您從不使用這個人任何地方。