English 中文(简体)
点击 UISearchbar 时 App 崩溃( 搜索 Display 控制器)
原标题:App crashes when clicking on UISearchbar (searchDisplayController)

我正在执行 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="此处输入图像描述"/"

问题回答

转到产品 & gt; edit schemets & gt; 启用 nszombie 对象并查看其中的示例

如果是转让属性, 您也许应该将其更改为保留属性 。 不要忘记将该属性设置为无, 在 < code> dealloc viewDidunload 中 。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...