iphone - UISearchBar 代表不响应取消按钮

标签 iphone objective-c ios4 uisearchbardelegate

我有一个 UIViewController,它是一个 UISearchBarDelegate 和一个 MKMapViewDelegatesearchBarSearchButtonClicked 事件工作正常,但在 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/

相关文章:

iphone - 我们可以从 iPhone 的联系人列表中访问电子邮件 ID 吗?

iphone - 使用 Core Data 时如何覆盖属性上的 getter?

ios - 如何在 xib 本地化之间切换

iphone - 如何使用 CFURLCreateFromFileSystemRepresentation

iphone - UIImagePickerController 基于旋转的相机 View 叠加

ios4 - UISearchBar 禁用自动禁用取消按钮

iPhone:了解现场崩溃报告:无法识别的选择器?

iphone - "Operation couldn' t 完成。连接由对等方重置。”在 iOS 上使用 NSURLConnection

ios - SDK不包含bitcode ios

ios-simulator - 如何在 iOS 4 上处理基于 OpenGL ES 的应用程序中的多任务处理?