views:

752

answers:

2
2009-08-19 11:00:06.482 Pickers[26090:20b] *** Assertion failure in -[UIDatePickerView _updateBitsForDate:andReload:animateIfNeeded:], /SourceCache/UIKit/UIKit-963.10/UIDatePicker.m:908
2009-08-19 11:00:06.483 Pickers[26090:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
2009-08-19 11:00:06.484 Pickers[26090:20b] Stack: (
    807902715,
    2513055291,
    807986683,
    811271572,
    816448278,
    816444436,
    816445688,
    10353,
    815223951,
    815274694,
    815269866,
    815277278,
    9116,
    814713539,
    814750709,
    814739251,
    814722434,
    814748641,
    839148405,
    807687520,
    807683624,
    814715661,
    814752238,
    8242
)

I keep getting that Error. I've tracked it down to the offending class, DatePickerViewController, because when I don't include that in the UITabBarController, It doesn't crash. The whole project compiles fine though. Here's DatePickerViewController:

/*
 File : DatePickerViewController.m
 Abstract: View Controller
 project Pickers

 Copyright (C) 2009 Mitchell Kember. All Rights Reserved.

 */

#import "DatePickerViewController.h"
#import "Model.h"


@implementation DatePickerViewController

@synthesize datePicker;


#pragma mark -
#pragma mark Setting up / Tearing down

- (id)init {
    if((self = [super init])) {
    }
    return self;
}

- (void)dealloc {
    [datePicker release];
    [super dealloc];
}

- (void)loadView {
    UIView *localView = [[UIView alloc] initWithFrame:
                              CGRectMake(0, 0, 320, 411)];
    [localView setBackgroundColor:[UIColor whiteColor]];
    [self setView:localView];
    [localView release];
}

- (void)viewDidLoad {
    UIDatePicker *localDatePicker = [[UIDatePicker alloc] initWithFrame:
         CGRectMake(0, 0, 320, 216)];
    // ======******OFFENDING LINE BELOW************=======
    [localDatePicker setDate:[[Model sharedModel] dateCache] animated:NO];
    /// =====**********========================*************===========
    [localDatePicker addTarget:self action:@selector(datePicker_ValueChanged:)
           forControlEvents:UIControlEventValueChanged];
    [self setDatePicker:localDatePicker];
    [localDatePicker release];

    UIButton *btnSelect = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnSelect setFrame:CGRectMake(100, 298, 120, 37)];
    [btnSelect setTitle:@"Select" forState:UIControlStateNormal];
    [btnSelect addTarget:self action:@selector(btnSelect_Pressed:)
     forControlEvents:UIControlEventTouchUpInside];

    [[self view] addSubview:[self datePicker]];
    [[self view] addSubview:btnSelect];

    [NSTimer scheduledTimerWithTimeInterval:0.3 target:self
     selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];

    [super viewDidLoad];
}

- (void)viewDidUnload {
    [self setDatePicker:nil];
    [super viewDidUnload];
}

#pragma mark -
#pragma mark Timer methods

- (void)timerFireMethod:(NSTimer *)theTimer {
    NSDate *now = [[NSDate alloc] init];
    [[self datePicker] setDate:now animated:YES];
    [now release];
    [theTimer invalidate];
}

#pragma mark -
#pragma mark Control actions

- (void)datePicker_ValueChanged:(id)sender {
    [[Model sharedModel] setDateCache:[[self datePicker] date]];
}

- (void)btnSelect_Pressed:(id)sender {
    NSString *selected = (NSString *)[[self datePicker] date];;
    NSString *msg = [[NSString alloc] initWithFormat:
     @"The date and time you selected is: %@", selected];
    UIAlertView *alert = [[UIAlertView alloc]
       initWithTitle:@"Date and Time Selected"
       message:msg
       delegate:nil 
      cancelButtonTitle:@"Yes, I did" 
      otherButtonTitles:nil];
    [msg release];
    [alert show];
    [alert release];
}

#pragma mark -
#pragma mark Memory warning

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

I'm pretty new to iPhone development. So any guesses on what I'm doing wrong? Thanks!!

EDIT: The offeding line that I pointed out in the code was causing it. I forgot to check if the value that I was trying to set the datePicker's date to was nil. And it was, so that was why it was crashing.

+1  A: 

Well somethign is wrong in your use of the DatePicker, its kind of hard to figure out with all that code you posted, is it possible you can step through the code and mark the line that causes the exception to be thrown?

Daniel
Ok I'm still working on it.. please check back again later when I have more info.
Mk12
Thanks for the fast answer :). I figured it out, it was because I didn't check if the date was nil.
Mk12
+1  A: 

You could try changing your datePicker_didChange method signature to something like this, but I don't really think that's the problem. I think it has something to do with the code inside your message handler calling [[self datePicker] date] and then casting the NSDate to an NSString. If you need a string, follow the Date / Time Formatting Guide.

It would probably help if had a human readable stack trace. Type "bt" or "backtrace" from the GDB console. Hope that helps :)

slf
Thanks, but I already solved it. The string/date thing wasn't the problem, although I have decided to change that to use an NSDateFormatter. Thanks!
Mk12