我目前正在开发一个应用程序,它有一些ViewController,上面有一个按钮,可以推送到带有搜索栏和搜索显示控制器的UITableViewController。我在单元格中有一些数据,并填充。我添加了以下代码来隐藏搜索栏,并在看到它时使其变为clearcolor:
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperView];
[self.searchBar setBackgroundColor:[UIColor clearColor]];
CGRect newBounds = [[self tableView]bounds];
newBounds.origin.y = newBounds.origin.y + searchBar.bounds.size.height;
[[self tableView] setBounds:newBounds];
现在这在我使用iOS模拟器时有效,但当我在我的设备上运行它并开始向上或向下滚动时,它会崩溃并给我以下错误:
EXC_BREAKPOINT (code = EXC_ARM_BREAKPOINT, subcode = 0xdefe)
然后我启用了Zombie Objects进一步调试并得到了这个:
-[UIView frame]: message sent to deallocated instance 0x156b1430
当我起飞时:
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperView];
[self.searchBar setBackgroundColor:[UIColor clearColor]];
再次在我的设备上运行我的应用程序,它不会崩溃并且工作正常。
任何人都有任何想法,这是怎么回事?提前谢谢!
答案 0 :(得分:0)
[[self.searchBar.subviews objectAtIndex:0] removeFromSuperView];
//This Line remove search bar from view ,which means it is deallocated at that moment so when you again try to remove it ,,the app crash ..
// So,instead of this try to hide searchbar ,,
Self. searchBar.hidden =YES;
答案 1 :(得分:0)
我认为UISearchBar对[self.searchBar.subviews objectAtIndex:0];
然后设置UITableView
的界限,然后更改UITableView
的边框。
它会触发自动调整大小和layoutSubviews
。
所以,UISearchBar的layoutSubviews方法访问[self.searchBar.subviews objectAtIndex:0].frame
用于布局或自动调整大小。
建议不要在SDK中更改UIControls
中的视图。
有一个想法是设置hidden=YES
而不是removeFromSuperview
。
答案 2 :(得分:0)
我遇到了同样的问题。进入这个页面,阅读'僵尸对象'在这个问题中,我记得我已经启用了僵尸对象。禁用它们后,问题就消失了。现在当我滚动我的tableView时,没有崩溃。
这可能很奇怪,但也许会帮助别人......