搜索栏取消按钮不起作用

时间:2011-05-09 06:26:56

标签: iphone objective-c cocoa-touch uisearchbar

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

这是我的搜索栏视图,看到取消按钮。它未启用

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;
}

这些是我的代表

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

我需要这样

enter image description here

或者我怎样才能在令人兴奋的取消按钮上添加另一个取消按钮。所以我可以启用它。请给我所有细节

4 个答案:

答案 0 :(得分:6)

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

 [mySearchDisplayController setActive:YES animated:YES];

或更简单:

 mySearchDisplayController.active = YES;

答案 1 :(得分:4)

我的猜测是,如果搜索文本字段为空或没有第一响应者,Apple会以取消按钮被禁用的方式创建UISearchBar。

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

如果您仍希望在显示视图时该按钮立即生效,则可以viewWillAppear:致电[mySearchBar becomeFirstResponder];

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

示例代码:

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

答案 2 :(得分:3)

这就是我所做的总是启用取消按钮,即使搜索字段不是第一响应者。

每当我在搜索字段

上调用resignFirstResponder时,我就会调用此方法
- (void)enableCancelButton {
    for (UIView *view in self.searchBar.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [(UIButton *)view setEnabled:YES];
        }
    }
}

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

答案 3 :(得分:0)

这可以重新启用iOS 8中的取消按钮:

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

    for subview in view.subviews {
        enableButtonsInSubviews(subview)
    }
}
相关问题