【发布时间】:2011-09-08 10:00:22
【问题描述】:
我有一个 UISearchBar 设置,当它被使用时,它有一个覆盖表的其余部分的覆盖灰色视图,类似于苹果的做法。
当我点击这个变暗的视图时,它会退出搜索模式,这很好,但是当我再次搜索,然后再次点击灰色退出时,它会因 EXC BAD ACCESS 错误而崩溃。
下面是一些相关代码:
崩溃总是在[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];
}
【问题讨论】:
-
你在哪一行得到这个错误?
-
崩溃总是在
[rvController doneSearching_Clicked:nil]; -
发布崩溃控制台日志中有关函数调用跟踪的内容。
-
谢谢,我在问题中添加了跟踪堆栈的屏幕截图。
-
控制台一定在给你一些信息,或者你怎么知道它是“EXC_BAD_ACCESS”
标签: iphone objective-c ipad exception-handling uisearchbar