I am working on a UIPickerController. I have everything working but when I tap the back button to go to my previous view the app is crashing.
I am thinking it could be catching a memory leak.
Here is my code. If anyone sees something I have missed or may have a clue on what cause this please inform me. Nothing displays in the Debugger.
@synthesize colorTextField, pickerView, pickerToolbar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[colorTextField release];
[pickerView release];
[pickerToolbar release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
colorsArray = [[NSMutableArray alloc] init];
[colorsArray addObject:@"Red"];
[colorsArray addObject:@"Orange"];
[colorsArray addObject:@"Yellow"];
[colorsArray addObject:@"Green"];
[colorsArray addObject:@"Blue"];
[colorsArray addObject:@"Indigo"];
[colorsArray addObject:@"Violet"];
}
- (void)setColor
{
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release];
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, actionSheet.bounds.size.width, 44)];
[pickerToolbar setBarStyle:UIBarStyleBlack];
[pickerToolbar sizeToFit];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(pickerDoneClicked)];
[pickerToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
[doneButton release];
[actionSheet addSubview:pickerToolbar];
[pickerToolbar release];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
- (void)pickerDoneClicked
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
[actionS开发者_StackOverflow社区heet release];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self setColor];
return NO;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [colorsArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [colorsArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"Selected Color: %@. Index of selected color: %i", [colorsArray objectAtIndex:row], row);
[colorTextField setText:(NSString *)[colorsArray objectAtIndex:row]];
}
- (void)viewDidUnload
{
[self setColorTextField:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
You're double-releasing pickerView
and pickerToolbar
, once in setColor
and again in dealloc
.
A good pattern when releasing views held in ivars is to set the ivar to nil after releasing the view, this will prevent double-releases.
I think your release some of your items mor ethan once EG:
in the dealloc you have two [pickerView release]; [pickerToolbar release];
that are release already eg: [actionSheet addSubview:pickerView]; [pickerView release];
so it has nothing to release me thinks, so remove the duplicate releases??
精彩评论