我正在执行 iPhone App 上的搜索显示控制器, 但当我试图点击搜索栏时会遇到以下错误( 经过几次尝试) 。
Thread 1: EXC_BAD_ACCESS (code=1, address=0x30000008)
""https://i.sstatic.net/lVq4v.png" alt="此处输入图像描述"/ >
我代码的片段如下:
- (void)viewDidLoad
{
//Setting up the search bar for search display controller
UISearchBar *tempBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 34, 320, 44)];
self.sBar = tempBar;
[tempBar release];
self.sBar.delegate = self;
self.sBar.tintColor = [UIColor colorWithHexString:@"#b6c0c7"];
self.sBar.placeholder = @"Search DM friends";
self.searchDisplayController = [[[UISearchDisplayController alloc] initWithSearchBar:sBar contentsController:self]autorelease];
[self setSearchDisplayController:searchDisplayController];
[searchDisplayController setDelegate:self];
[searchDisplayController setSearchResultsDataSource:self];
self.searchDisplayController.searchResultsTableView.delegate = self;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 78)]autorelease];
headerView.backgroundColor = [UIColor colorWithHexString:@"#ebe7e6"];
if (tableView != self.searchDisplayController.searchResultsTableView){
//Search
UILabel *tagFriendsTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 320, 16)];
tagFriendsTitle.font = [UIFont boldSystemFontOfSize:14];
tagFriendsTitle.backgroundColor = [UIColor clearColor];
tagFriendsTitle.text = @"Who should see this? Tag them!";
[headerView addSubview:tagFriendsTitle];
//THIS IS WHERE I GET MY EXC_BAD_ACCESS error
[headerView addSubview:self.sBar];
[tagFriendsTitle release];
}
return headerView;
}
我不知道我代码的哪个部分导致错误, 但是在我试图将其添加到页眉子视图时, 似乎从记忆中找到的 sBar 交易 。 但是我不知道为什么我需要点击搜索栏多次才能发生这种情况 。
这是它如何看待 iPhone 的 iPhone, 搜索栏是头视图的一部分 。
""https://i.sstatic.net/NMojj.png" alt="此处输入图像描述"/"