调用在子视图中使用的DatePicker后的错误访问

时间:2015-03-06 19:33:15

标签: ios objective-c datepicker exc-bad-access

我继承了使用DatePicker的iOS应用。创建datepicker视图的控制器代码是:

//Create the date picker view set the delegate to self and add the subview
DateViewController* date = [[DateViewController alloc]init];
date.delegate = self;
[self.view addSubview:[date.view autorelease]];

DateViewController中用于选择数据的代码如下:

- (IBAction)btnDate:(id)sender 
{
    NSLog(@"Button Pushed");
    if ([delegate respondsToSelector:@selector(getDate:)]) 
    {
        NSLog(@"Responding to selector");
        //send the delegate function with the amount entered by the user
        NSDate *selDate = [pkrDate date];
        NSCalendar *greg = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *components = [greg components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:selDate];
        [greg release];

        int selectedDate = [components year] * 10000 + [components month] * 100 + [components day];
        NSLog(@"%i",selectedDate);

        //int to nsinteger problem?
        [delegate getDate:selectedDate];
    }
    //Remove view
    [self.view removeFromSuperview];
}

这是控制器视图中的getDate函数:

//Function from delegate
-(void)getDate:(NSInteger)date
{
    //Store the date selected
    NSLog(@"The date: %i",date);

    [self setStrDate:nil];


//    NSLog(@"%@",strDate);
    //Display the date in the text box
    int year = date / 10000;
    int month = (date - (year * 10000)) / 100;
    int day = date - (year *10000) - (month * 100);

    //month and day must have preceding zero
    NSString* strMonth;
    NSString* strDay;
    if (month < 10) 
    {
        strMonth = [NSString stringWithFormat: @"0%i",month];
    }
    else 
    {
        strMonth = [NSString stringWithFormat:@"%i",month];
    }

    if (day < 10) 
    {
        strDay = [NSString stringWithFormat: @"0%i",day];
    }
    else 
    {
        strDay = [NSString stringWithFormat:@"%i",day];
    }

    strDate = [[NSString alloc]initWithString:[NSString stringWithFormat: @"%i%@%@",year,strMonth,strDay]];
    //strDate = [[NSString alloc]initWithString:[NSString stringWithFormat: @"%i%@%@",year,strMonth,strDay]];


    txtDate.textColor = [UIColor blackColor];
    txtDate.text = [NSString stringWithFormat:@"%i/%i/%i",month,day,year];
}

通过上述方法没有问题。但是,它会在调用UIApplicationMain的主循环中崩溃。

int main(int argc, char *argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

在此错误消息上:

Thread 1: EXC_BAD_ACCESS (code=1, address=0xa00000010). 

为什么会发生这种情况?

0 个答案:

没有答案