搜索栏取消按钮不起作用

发布于 2024-11-05 12:54:51 字数 2002 浏览 2 评论 0原文

我的应用程序有一个搜索栏,用于从表视图中搜索记录,该表视图由 sqlite DB 填充。 我的问题是,当视图打开时,“取消”按钮未启用,而且我也无法触摸它,就像仅图像一样。它在那里,但没有任何操作。 当我们单击该搜索栏文本时,取消按钮将更改为“完成”,它已启用。 所以这是我的代码,

这是我的搜索栏视图,请参阅取消按钮。它未启用

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    //[newSearchBar setShowsCancelButton:YES animated:YES];


    newSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

    NSLog(@"search begin edit") ;

    //searchString = searchBar.text;

    //NSLog(@"print did edit searchstring : %@", searchString) ;

    for(UIView *view in [searchBar subviews])       
    {

        //shareItemId =newSearchBar.text;

        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            [(UIBarItem *)view setTitle:@"Done"];

        }
        }

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"searchBarTextDidEndEditing:");


    [searchBar resignFirstResponder];
    //[self dismissModalViewControllerAnimated:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"searchBarSearchButtonClicked");


    searchString = searchBar.text;
    NSLog(@"search %@", searchBar.text);
    [newSearchBar setShowsCancelButton:NO animated:YES];

    [searchBar resignFirstResponder];
    //[self dismissModalViewControllerAnimated:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{  
    NSLog(@" searchBarCancelButtonClicked");


    [searchBar resignFirstResponder];

     shareItemName =newSearchBar.text;

    [self dismissModalViewControllerAnimated:YES];

}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    NSLog(@"searchBarShouldBeginEditing");


    [newSearchBar setShowsCancelButton:YES animated:YES];
    return YES;
}

这些是我的代表,

请检查我的代码并给我答案。我需要在加载视图时启用“取消”按钮,它的操作将返回到

我需要的

上一个视图在此处输入图像描述

否则我如何在令人兴奋的取消按钮上添加另一个取消按钮。以便我可以启用该按钮。请给我所有详细信息

My App is having a search bar for searching records from the table view,which is populated by sqlite DB.
My problem is that when the view opens the "cancel" button is not enabled and also I cant touch on that, just like a image only.It is there but no action is with that.
when we click on that search bar text the cancel button will be changed to "done" it is enabled one.
so here is my code

this is my search bar view,see that cancel button.It is not enabled

this is my search bar view.see that cancel button.It is not enabled

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    //[newSearchBar setShowsCancelButton:YES animated:YES];


    newSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;

    NSLog(@"search begin edit") ;

    //searchString = searchBar.text;

    //NSLog(@"print did edit searchstring : %@", searchString) ;

    for(UIView *view in [searchBar subviews])       
    {

        //shareItemId =newSearchBar.text;

        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            [(UIBarItem *)view setTitle:@"Done"];

        }
        }

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"searchBarTextDidEndEditing:");


    [searchBar resignFirstResponder];
    //[self dismissModalViewControllerAnimated:YES];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"searchBarSearchButtonClicked");


    searchString = searchBar.text;
    NSLog(@"search %@", searchBar.text);
    [newSearchBar setShowsCancelButton:NO animated:YES];

    [searchBar resignFirstResponder];
    //[self dismissModalViewControllerAnimated:YES];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

{  
    NSLog(@" searchBarCancelButtonClicked");


    [searchBar resignFirstResponder];

     shareItemName =newSearchBar.text;

    [self dismissModalViewControllerAnimated:YES];

}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    NSLog(@"searchBarShouldBeginEditing");


    [newSearchBar setShowsCancelButton:YES animated:YES];
    return YES;
}

These are my delegates for that

Please check my code and give me the answer. I need to enable the "Cancel" button when the view is loaded and it action will be go back to previous view

I need like this

enter image description here

Or else how can I add a another cancel button on exciting cancel button.so that I can enable that.please give me all the details

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

还如梦归 2024-11-12 12:54:51

您需要将 UISearchDisplayController 设置为 ACTIVE,如下所示:

 [mySearchDisplayController setActive:YES animated:YES];

或更简单:

 mySearchDisplayController.active = YES;

You need to set the UISearchDisplayController to be ACTIVE, like this:

 [mySearchDisplayController setActive:YES animated:YES];

or more simply:

 mySearchDisplayController.active = YES;
时光病人 2024-11-12 12:54:51

我的猜测是,Apple 制作 UISearchBar 的方式是,如果搜索文本字段为空或不是第一响应者,则取消按钮将被禁用。

这是有道理的,因为除了实际取消搜索之外,您不应该将“取消”按钮用于其他目的。并且由于没有要取消的搜索 - 该按钮被禁用。

如果您仍然希望按钮在视图呈现时立即处于活动状态,您可以在 viewWillAppear: 处调用 [mySearchBarBecomeFirstResponder];

这将导致键盘出现并且该按钮将被启用。
然后,如果用户点击取消,您可以拦截它以返回到上一个视图。 (我不确定苹果是否会喜欢这种行为)。

示例代码:

-(void) viewWillAppear : (BOOL) animated
{
    [super viewWillAppear:animated];
    // Make keyboard pop and enable the "Cancel" button.
    [self.mySearchBar becomeFirstResponder];
}

My guess is that Apple made the UISearchBar in a way that the cancel button is disabled if the search text field is empty or not first responder.

This is make sense because you should not use the "Cancel" button to other purpose than actually canceling the search. and since there is no search to cancel - the button is disabled.

If you still want that the button will be active immediately when the view is presented, you can call at viewWillAppear: to [mySearchBar becomeFirstResponder];

This will cause to the keyboard to appear and the button will be enabled.
And then if the user hit cancel you can intercept it to go back to the previous view. (I'm not sure if apple will like this behavior).

Sample code:

-(void) viewWillAppear : (BOOL) animated
{
    [super viewWillAppear:animated];
    // Make keyboard pop and enable the "Cancel" button.
    [self.mySearchBar becomeFirstResponder];
}
没有心的人 2024-11-12 12:54:51

这是我为始终启用取消按钮所做的操作,即使搜索字段不是第一响应者也是如此。

每当我在搜索字段上调用 ​​resignFirstResponder 时,我都会调用此方法。

- (void)enableCancelButton {
    for (UIView *view in self.searchBar.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIButton *)view setEnabled:YES];
        }
    }
}

这可行,但我不确定它是否会通过 App Store 验证,因此使用它需要您自担风险。此外,这可能仅在取消按钮是您在搜索字段中使用的唯一按钮时才有效。

Here's what I did to always enable the cancel button, even when the search field is not first responder.

I'm calling this method whenever I call resignFirstResponder on the search field

- (void)enableCancelButton {
    for (UIView *view in self.searchBar.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIButton *)view setEnabled:YES];
        }
    }
}

This works, but I'm not sure whether it will pass App Store verification yet, so use it at your own risk. Also, this probably only works if the cancel button is the only button you are using with the search field.

月依秋水 2024-11-12 12:54:51

从 iOS 8 开始,这可以重新启用取消按钮:

private func enableButtonsInSubviews(view: UIView) {
    if let view = view as? UIButton {
        view.enabled = true
    }

    for subview in view.subviews {
        enableButtonsInSubviews(subview)
    }
}

This works to reenable the cancel button as of iOS 8:

private func enableButtonsInSubviews(view: UIView) {
    if let view = view as? UIButton {
        view.enabled = true
    }

    for subview in view.subviews {
        enableButtonsInSubviews(subview)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文