iPhone开发 - NSInternalInconsistencyException有帮助吗?

时间:2009-08-19 15:07:14

标签: iphone objective-c cocoa-touch exception

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
)

我一直得到那个错误。我已将其跟踪到违规类DatePickerViewController,因为当我不在UITabBarController中包含它时,它不会崩溃。整个项目编译得很好。这是DatePickerViewController:

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

 Copyright (C) 2009 Mitchell K. 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

我是iPhone开发的新手。我猜错了什么?谢谢!

编辑:我在代码中指出的种子线是造成它的。我忘了检查我试图将datePicker的日期设置为的值是否为零。它就是,所以这就是崩溃的原因。

2 个答案:

答案 0 :(得分:2)

你使用DatePicker时有些问题是错误的,你发布的所有代码很难弄明白,你是否可以单步执行代码并标记导致异常抛出的行?

答案 1 :(得分:1)

您可以尝试将datePicker_didChange方法签名更改为something like this,但我并不认为这是问题所在。我认为这与您调用[[self datePicker] date]的消息处理程序中的代码有关,然后将NSDate转换为NSString。如果您需要字符串,请按照Date / Time Formatting Guide

如果有一个人类可读的堆栈跟踪可能会有所帮助。从GDB控制台键入“bt”或“backtrace”。希望有所帮助:)