2013-03-05 187 views
2

我已經在幾個小時前創建了一篇文章,但這是不正確的,我誤解了導致崩潰的事情。所以我轉發了它,我希望你不會爲此責怪我。無法正確設置datePicker的默認日期,在iOS設備上崩潰

我得到一個擁有一個datePicker的觀點,我的觀點是在選擇器上顯示NOT當前日期,但顯示「舊」日期(我選擇1987年)。但我不知道什麼出了問題,當我在模擬器上啓動應用程序時,它工作得很好,但是當我在加載視圖之前切換到iPhone應用程序崩潰時。還有就是代碼:

-(void)defaultBirthdayPickerDate{ 

    NSString *dateString = @"09-Oct-1987"; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    dateFormatter.dateFormat = @"dd-MMM-yyyy"; 
    NSDate *date = [dateFormatter dateFromString:dateString]; 
    [self.birthdayPicker setDate:date]; 

} 

然後:

- (void)viewDidLoad 
{ 

    [self defaultBirthdayPickerDate]; 
    [super viewDidLoad]; 
} 

有東西,什麼地方的東西導致崩潰:

2013-03-05 15:50:31.070 DeathLine[2985:907] *** Assertion failure in -[_UIDatePickerView _setDate:animated:forced:], /SourceCache/UIKit/UIKit-2380.17/_UIDatePickerView.m:302 
2013-03-05 15:50:31.073 DeathLine[2985:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date' 
*** First throw call stack: 
(0x325042a3 0x3a22197f 0x3250415d 0x32dd9ab7 0x344bdf15 0x344c6429 0x3458f989 0x14a6b 0x148db 0x3432b595 0x3438014b 0x34380091 0x3437ff75 0x3437fe99 0x3437f5d9 0x3437f4c1 0x3436db93 0x28f4c33 0x3436d833 0x343f70c5 0x343f7077 0x343f7055 0x343f690b 0x343f6e01 0x3431f5f1 0x3430c801 0x3430c11b 0x360225a3 0x360221d3 0x324d9173 0x324d9117 0x324d7f99 0x3244aebd 0x3244ad49 0x360212eb 0x34360301 0xcc21 0x3a658b20) 
libc++abi.dylib: terminate called throwing an exception 

不知[self.birthdayPicker.date]不爲空,我知道,但我不知道如何解決問題。

+1

日期看起來像什麼時候你nslog它? NSLog(@「date:%@」,date);和/或調試器如何呈現它? – 2013-03-05 11:58:32

+0

Yeah..2013-03-05 16:01:23.727 DeathLine [3003:907] date:(null)。謝謝Hermann – 2013-03-05 12:02:48

回答

3

我的猜測是,這是由您的設備上設置diffent區域(NSLocale),並在模擬器引起。請注意,09-Oct-1987日期只能在格式化程序區域設置爲英語時由您的格式程序解析(因爲Oct代表October英文)。否則它將返回nil並將nil設置爲UIDatePicker將導致您看到的錯誤。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"]; 
+0

是的,它的工作..非常感謝你,蘇丹! :) – 2013-03-05 12:20:29

1

date可能是nil。您應該檢查格式化程序是否按預期工作。

+0

是的.. 2013-03-05 16:01:23.727 DeathLine [3003:907] date:(null)。但是如何以及爲什麼它在模擬器上的作品呢?當我在模擬器上啓動它時,它是2013-03-05 16:02:57.920 DeathLine [6155:11303] date:1987-10-08 21:00:00 +0000 – 2013-03-05 12:02:27

1

試試這個

-(void)defaultBirthdayPickerDate{ 
    [self.birthdayPicker setDatePickerMode:UIDatePickerModeDate]; 
    NSString *dateString = @"09-Oct-1987"; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd-MMM-yyyy"]; 
    if([dateFormat dateFromString: dateString]) 
    { 
     NSDate *date = [dateFormat dateFromString: dateString]; 
     [self.birthdayPicker setDate:date]; 
    } 
    [dateFormat release]; 
} 

- (void)viewDidLoad{ 
    [super viewDidLoad]; //super method should be first in you code 
    [self defaultBirthdayPickerDate]; 
} 

嘗試設置DatePickerMode到UIDatePickerModeDate這可能會幫助您解決問題

+0

謝謝阿克巴里,但我已經解決了這個 – 2013-03-05 12:21:02