【问题标题】:Can't find the cause of my app crashing找不到我的应用程序崩溃的原因
【发布时间】:2011-11-14 03:10:39
【问题描述】:

我正在开发 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

【问题讨论】:

  • 如果您在发布的代码中更具选择性,它可以帮助您和其他用户。例如,这里的 initWithNibName:bundle: 和 didReceiveMemoryWarning 代码只是模板代码。无需发布它们。您可能还可以确信,鉴于您问题的性质,不需要包含 shouldAutoroateToInterfaceOrientation: 方法。

标签: ios memory-leaks nsmutablearray uipickerview


【解决方案1】:

您将双重释放 pickerViewpickerToolbar,一次在 setColor 中,然后在 dealloc 中。

释放 ivars 中持有的视图时,一个好的模式是在释放视图后将 ivar 设置为 nil,这样可以防止双重释放。

【讨论】:

  • 非常感谢大家!我想这只是我的视线。谁能帮我解释一下如何设置多个 UIPicker 的?会不会只使用某种 if 语句?
  • @LukeIrvin:什么,多个选择器拥有同一个代表?是的,只需在委托方法中执行 if 语句来测试 thePickerView 以确定正在使用哪个选择器。如果您需要更多帮助,请随时发布新问题。
【解决方案2】:

我认为你不止一次发布了一些你的项目 例如:

在 dealloc 中你有两个 [pickerView 发布]; [pickerToolbar 发布];

已经发布 例如: [actionSheet addSubview:pickerView]; [pickerView 发布];

所以它没有什么可以释放我的想法,所以删除重复的版本??

【讨论】:

    猜你喜欢
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多