我有一个 UIViewController
,它是一个 UISearchBarDelegate
和一个 MKMapViewDelegate
。 searchBarSearchButtonClicked
事件工作正常,但在 iOS 4.2 中测试时,当点击取消按钮时,searchBarCancelButtonClicked 永远不会被调用。在 4.3 中一切正常。我有其他具有相同代码的 View ,并且工作正常。我已经三次检查了方法签名。
这可能与 MapView 有关,还是我做错了什么?
我的 .h 文件:
@interface MyViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate,UIAlertViewDelegate>{
MKMapView *mapMainView;
UISearchBar *sBar;
}
@property (nonatomic, retain) UISearchBar *sBar;
@end
然后我像这样创建搜索栏:
sBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)] autorelease];
sBar.delegate = self;
sBar.showsCancelButton = YES;
[self.view addSubview:sBar];
[sBar becomeFirstResponder];
方法:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
searchBar.hidden = YES;
}
有没有人知道为什么会发生这种情况?
最佳答案
我遇到了完全相同的问题。按住取消按钮几秒钟有效。
我的原因是我在 TableView 中实现了 UITapGestureRecognizer。因此,这优先于搜索栏中的按钮点击或“x”按钮点击。
在我的案例中,解决方案是将手势识别限制在 tableview 的背景 View 中。我猜你的情况可能会发生类似的事情。尝试将手势识别器限制为所需的最小 subview ,并且搜索栏应该在该 View 之外。
关于iphone - UISearchBar 代表不响应取消按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6004438/