使用UISearchBar方法使EXC_BAD_ACCESS崩溃

时间:2011-09-07 17:59:16

标签: iphone objective-c ipad exception-handling uisearchbar

我有一个UISearchBar设置,当它被使用时,它有一个覆盖灰色视图,覆盖表格的其余部分,类似于苹果的方式。

当我点击这个黑暗的视图时,它会退出搜索模式,这很好,但是当我再次搜索时,再次单击灰色退出,它会因EXC BAD ACCESS错误而崩溃。

以下是一些相关代码:

enter image description here

崩溃总是在[rvController doneSearching_Clicked:nil];

OverlayViewController.h

@interface OverlayViewController : UIViewController

@property (nonatomic, retain) ChooserViewController *rvController;
@property (nonatomic, retain) CPTViewController *cptViewController;


@implementation OverlayViewController

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [rvController doneSearching_Clicked:nil];
    [cptViewController doneSearching_Clicked:nil];
}

- (void) dealloc {
    [rvController release];
    [cptListViewController release];
    [super dealloc];
}

@end

带搜索栏的实际VC代码

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar
{
    searching = YES;
    [ovController.view removeFromSuperview];
    letUserSelectRow = YES;
    self.tableView.scrollEnabled = YES;
    if (self.mySearchBar.text !=nil)
    {
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"(name contains[cd] %@) OR (code contains[cd] %@)", self.mySearchBar.text, self.mySearchBar.text];
        [fetchedResultsController.fetchRequest setPredicate:predicate];
    }
    else
    {
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"All"];
        [fetchedResultsController.fetchRequest setPredicate:predicate];
    }

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) 
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    } 

    self.searchArray = fetchedResultsController.fetchedObjects;

    [mySearchBar resignFirstResponder];

    [self.tableView reloadData];
}

- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.navigationItem.leftBarButtonItem = nil;

    if(searching)
        return;

    if(ovController == nil)
        ovController = [[OverlayViewController alloc] initWithNibName:@"OverlayView" bundle:[NSBundle mainBundle]];

    CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;

    CGRect frame = CGRectMake(0, yaxis, width, height);
    ovController.view.frame = frame;    
    ovController.view.backgroundColor = [UIColor grayColor];
    ovController.view.alpha = 0.5;

    ovController.rvContrller = self;

    [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];

    searching = YES;
    letUserSelectRow = NO;
    self.tableView.scrollEnabled = NO;

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] 
                                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                                               target:self action:@selector(doneSearching_Clicked:)] autorelease];
}

- (void) searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = NO;
}

- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText 
{
    [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];

    searching = NO;
    letUserSelectRow = NO;
    self.tableView.scrollEnabled = NO;

    [self.tableView reloadData];
}
- (void) doneSearching_Clicked:(id)sender {

    mySearchBar.text = @"";
    [mySearchBar resignFirstResponder];

    letUserSelectRow = YES;
    searching = NO;
    self.navigationItem.rightBarButtonItem = nil;
    self.tableView.scrollEnabled = YES;

    [ovController.view removeFromSuperview];
    [ovController release];
    ovController = nil;

    [self.tableView reloadData];
    [self.tableView scrollToRowAtIndexPath:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

2 个答案:

答案 0 :(得分:0)

您的代码并非完全不言自明,但我猜您过度释放ovController。您使用ovController的{​​{1}}方法,然后让它在另一个对象touchesBegan:中执行一个方法,释放调用对象。尝试从方法调用返回到已发布的控制器时,会出现错误访问错误。

答案 1 :(得分:0)

/// @OverlayView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [rvController doneSearching_Clicked:nil]; ///< Point 1
    [cptViewController doneSearching_Clicked:nil];
}

/// @VC Search bar
- (void) doneSearching_Clicked:(id)sender {

    mySearchBar.text = @"";
    [mySearchBar resignFirstResponder];

    letUserSelectRow = YES;
    searching = NO;
    self.navigationItem.rightBarButtonItem = nil;
    self.tableView.scrollEnabled = YES;

    /// Point 2 
    [ovController.view removeFromSuperview];
    [ovController release];
    ovController = nil;

    [self.tableView reloadData];
    [self.tableView scrollToRowAtIndexPath:0 atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

Point 1,它会调用-(void)doneSearching_Clicked,然后执行Point 2。在Point 2,我认为OverlayView对象的retainCount将为0,这意味着它将被解除分配。因此,OverlayView所有指针都可能无效,因为它会在-(void)dealloc中释放这些对象。根据你的密码,这就是我所能知道的。

编辑:
    上述程序可以翻译成以下格式。 -doneSearching_Click会导致[self release]行为。

/// @OverlayView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [rvController doneSearching_Clicked:nil]; ///< Point 1
    /// [self release];  /// the above method will do such thing.
    [cptViewController doneSearching_Clicked:nil];
}