UISearchBar 委托不响应取消按钮

发布于 2024-11-07 10:27:32 字数 993 浏览 1 评论 0原文

我有一个 UIViewController,它是一个 UISearchBarDelegate 和一个 MKMapViewDelegatesearchBarSearchButtonClicked 事件工作正常,但在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浊酒尽余欢 2024-11-14 10:27:32

我遇到了完全相同的问题。按住取消按钮几秒钟就可以了。

对我来说,原因是我在表视图中实现了 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.

甜味超标? 2024-11-14 10:27:32

可能您的 sbar 对象正在释放,在本例中是一个自动释放对象,为什么?尝试将 sBar 声明为 IBOutlet 属性。在 Interface Builder 中创建适当的链接,在编码时删除分配,放入 viewDidUnload
self.sbar = nil;
并在 dealloc 中释放它。在 viewDidLoad 中放置这个。

sBar.delegate = self;
sBar.showsCancelButton = YES; // this is an option in object inspector
[self.view addSubview:sBar];
[sBar becomeFirstResponder]; //remove this.

告诉我是否有效

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.

sBar.delegate = self;
sBar.showsCancelButton = YES; // this is an option in object inspector
[self.view addSubview:sBar];
[sBar becomeFirstResponder]; //remove this.

Tell me if it works

海螺姑娘 2024-11-14 10:27:32

试试这个:

sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)];

sBar.delegate = self;

sBar.showsCancelButton = YES;

[self.view addSubview:sBar];

并尝试将release放入dealloc中

try this:

sBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 70.0)];

sBar.delegate = self;

sBar.showsCancelButton = YES;

[self.view addSubview:sBar];

and try to put release in dealloc

情栀口红 2024-11-14 10:27:32
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar resignFirstResponder];
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        [self.searchDisplayController setActive:NO animated:YES];
        [self.searchDisplayController.searchBar resignFirstResponder];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文