单击UISearchbar(searchDisplayController)时应用程序崩溃

时间:2012-05-22 14:26:57

标签: iphone ios crash uisearchbar uisearchdisplaycontroller

我正在iPhone App上实现searchdisplay控制器,但是当我尝试点击搜索栏时(尝试几次后)会出现以下错误

Thread 1: EXC_BAD_ACCESS (code=1, address=0x30000008)

enter image description here

我的代码片段如下:

- (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上的外观,搜索栏构成了headerview的一部分

enter image description here

2 个答案:

答案 0 :(得分:3)

转到产品>编辑方案>启用nszombie对象并查看那里的问题

答案 1 :(得分:1)

如果这是一个assign属性,您应该将其更改为retain属性。并且不要忘记在deallocviewDidUnload中将属性设置为nil。