UISearchBar 委托不响应取消按钮
我有一个 UIViewController
,它是一个 UISearchBarDelegate
和一个 MKMapViewDelegate
。 searchBarSearchButtonClicked
事件工作正常,但在 iOS 4.2 中测试时,按下取消按钮时永远不会调用 searchBarCancelButtonClicked。在 4.3 中一切正常。我有其他具有相同代码的视图,并且工作正常。我已经三次检查了方法签名。
这可能与 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;
}
有人知道为什么会发生这种情况吗?
I have a UIViewController
that is a UISearchBarDelegate
and a MKMapViewDelegate
. The searchBarSearchButtonClicked
event works fine, but when testing in iOS 4.2 the searchBarCancelButtonClicked never gets called when hitting the cancel button. In 4.3 everything works fine. I have other views with identical code and it works fine. I have triple checked the method signatures.
Could it be something to do with the MapView, or am I doing something blatantly wrong?
My .h file:
@interface MyViewController : UIViewController <UISearchBarDelegate,MKMapViewDelegate,UIAlertViewDelegate>{
MKMapView *mapMainView;
UISearchBar *sBar;
}
@property (nonatomic, retain) UISearchBar *sBar;
@end
And I create the search bar like so:
sBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)] autorelease];
sBar.delegate = self;
sBar.showsCancelButton = YES;
[self.view addSubview:sBar];
[sBar becomeFirstResponder];
The method:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
searchBar.hidden = YES;
}
Does anyone have an idea of why this may be happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了完全相同的问题。按住取消按钮几秒钟就可以了。
对我来说,原因是我在表视图中实现了 UITapGestureRecognizer。因此,这优先于搜索栏中的按钮单击或“x”按钮单击。
我的解决方案是将手势识别限制为仅表格视图的背景视图。我猜你的情况可能会发生类似的事情。尝试将手势识别器限制为所需的最小子视图,并且搜索栏应位于该视图之外。
I had the exact same problem. Holding the cancel button for a few seconds worked.
The reason for me was that I had implemented UITapGestureRecognizer in the tableview. So this took precedence over the button click or 'x' button click in the search bar.
The solution in my case was to restrict the gesture recognition to only the backgroundview of the tableview. I guess similar thing might be happening in your case. Try to restrict the gesture recognizers to the minimum subview required and the search bar should be outside that view.
可能您的 sbar 对象正在释放,在本例中是一个自动释放对象,为什么?尝试将 sBar 声明为 IBOutlet 属性。在 Interface Builder 中创建适当的链接,在编码时删除分配,放入 viewDidUnload
self.sbar = nil;
并在 dealloc 中释放它。在 viewDidLoad 中放置这个。
告诉我是否有效
Probably your sbar object are releasing, in this case is an autorelease object, Why ?. Try declaring sBar as IBOutlet property. Make the apropiate links in the Interface Builder, remove the alloc as you code it, put in viewDidUnload
self.sbar = nil;
and releas it in dealloc. in viewDidLoad put this.
Tell me if it works
试试这个:
并尝试将release放入dealloc中
try this:
and try to put release in dealloc