2011-09-09 4 views
0

UIPickerController에서 작업 중입니다. 모든 것이 작동하지만 이전 버튼을 눌러 이전보기로 이동하면 앱이 다운됩니다.내 응용 프로그램 충돌의 원인을 찾을 수 없습니다.

메모리 누수가 발생할 수 있다고 생각합니다.

여기 내 코드입니다. 누군가 내가 본 것을 놓친 경우 또는 원인에 대한 단서가있을 수 있습니다. 디버거에 아무 것도 표시되지 않습니다. 두 [pickerView 해제]를 가지고있는 할당 해제에

;

@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

게시하는 코드를 약간만 선택하면 다른 사용자에게 도움이됩니다. 예를 들어, initWithNibName : bundle : 및 didReceiveMemoryWarning 코드는 템플릿 코드 일뿐입니다. 게시 할 필요가 없습니다. shouldAutoroateToInterfaceOrientation : 메서드를 질문의 성격에 포함시킬 필요가 없다고 확신 할 수 있습니다. – Obliquely

답변

0

나는 당신의 자료 항목의 MOR의 에탄의 일부 한번 EG 생각 [pickerToolbar release];

이미 출시 중입니다. 예 : [actionSheet addSubview : pickerView]; [pickerView release];

그래서 그것은 나를 석방 할 생각이 없으므로 복제본을 제거 하시겠습니까 ??

2

pickerViewpickerToolbarsetColor에 다시 한 번 두어 dealloc 번에 두 번 출시했습니다.

ivars에서 보유한보기를 해제 할 때보기를 해제 한 후에 ivar을 nil로 설정하는 것이 좋은 패턴입니다. 이중 분리를 방지 할 수 있습니다.

+0

고마워요! 나는 이것이 내 시야를 미끄러 뜨렸다 고 생각한다. 아무도 나에게 UIPicker를 여러 개 설치하는 방법을 설명해 줄 수 있습니까? if 문을 사용하면 될까요? –

+0

@ LukeIrvin : 같은 대리인이있는 여러 피커? 예, 델리게이트 메소드의 if 문을 사용하여 'thePickerView'를 테스트하여 사용중인 피커를 판별하십시오. 더 많은 도움이 필요하면 자유롭게 새로운 질문을 올리십시오. – Anomie

관련 문제