我正在研究UIPickerController。我有一切正常工作但当我点击后退按钮转到我之前的视图时,应用程序崩溃了。
我在想它可能会导致内存泄漏。
这是我的代码。如果有人看到我错过的东西,或者可能有什么原因,请告知我。调试器中没有显示任何内容。
@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];
[actionSheet 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
答案 0 :(得分:2)
您在pickerView
和pickerToolbar
中再次发布setColor
和dealloc
。
释放ivars中的视图时,一个好的模式是在释放视图后将ivar设置为nil,这样可以防止双重释放。
答案 1 :(得分:0)
我认为你曾经发布了一些你的项目 EG:
在dealloc你有两个 [pickerView发布]; [pickerToolbar发布];
已经发布了 例如: [actionSheet addSubview:pickerView]; [pickerView发布];
所以它没有什么可以释放我的想法,所以删除重复版本??