在iOS 7中,在UISearchBar中设置UITextField的委托似乎打破了两者。我怎样才能让两位代表回应?

时间:2013-10-30 20:13:36

标签: delegates ios7 uitextfield uisearchbar uitextfielddelegate

我有一个搜索栏,可以在用户输入时过滤联系人,但我还需要直接访问文本字段,以防他们输入不在地址簿中的名称。所以,我需要让UISearchBarDelegate和UITextFieldDelegate同时工作。

在iOS 6中,这很好用。在iOS 7中,一旦我找到搜索栏文本字段并设置其委托,一切都会中断。键盘不再响应,不显示文字等。

这是视图出现时的代码:

UITextField *searchBarTextField = nil;
for (UIView *searchBarSubview in [mySearchBar subviews]) {
    if ( [searchBarSubview isKindOfClass:[UITextField class] ] ) {
        // ios 6 and earlier
        searchBarTextField = (UITextField *)searchBarSubview;
    } else {
        // for ios 7 what we need is nested inside another container
        for (UIView *subSubView in [searchBarSubview subviews]) {
            if ( [subSubView isKindOfClass:[UITextField class] ] ) {
                searchBarTextField = (UITextField *)subSubView;
            }
        }
    }
}
if (searchBarTextField) {
    [searchBarTextField setReturnKeyType:UIReturnKeyNext];
    // TODO: in ios 7, setting this breaks everything. search stops working, search keyboard no longer responds, nothing.
    [searchBarTextField setDelegate:self];
}

我不知所措。任何人都知道如何在iOS 7中使用它?感谢。

3 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我找到了一个丑陋的解决方案,在附件视图中,在键盘顶部创建了一个完成按钮。没什么好的。

enter image description here

for(UIView *subView in _searchBar.subviews) {

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

        for (UIView *subSubView in subView.subviews) {

            if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
                UITextField *textField = subSubView;
                UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
                UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(done:)];
                toolbar.items = @[done];
                textField.inputAccessoryView = toolbar;
                _theTextField = textField;
            }
        }
    } else {

        if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
            UITextField *textField = subView;
            textField.returnKeyType = UIReturnKeyDone;
            textField.delegate = self;
        }
    }
}

- (void)done:(UIBarButtonItem *)barButtonItem
{
    [self textFieldShouldReturn:_theTextField];
}

答案 1 :(得分:0)

for (UIView *searchSubs in searchBar.subviews){
    for (id searchSub in searchSubs.subviews) {
        if ([searchSub conformsToProtocol:@protocol(UITextInputTraits)]){
            [(UITextField *)searchSub setReturnKeyType:UIReturnKeyDone];
            [(UITextField *)searchSub setKeyboardAppearance:UIKeyboardAppearanceAlert];
            [(UITextField *)searchSub setEnablesReturnKeyAutomatically:NO];
        }
    }
}

答案 2 :(得分:0)

我认为这可能是在子视图循环之后将委托设置为UITextField的范围问题。尝试在循环内设置委托:

    UITextField *searchBarTextField = nil;
for (UIView *searchBarSubview in [mySearchBar subviews]) {
    if ( [searchBarSubview isKindOfClass:[UITextField class] ] ) {
        // ios 6 and earlier
        searchBarTextField = (UITextField *)searchBarSubview;
        searchBarTextField.delegate = self;
    } else {
        // for ios 7 what we need is nested inside another container
        for (UIView *subSubView in [searchBarSubview subviews]) {
            if ( [subSubView isKindOfClass:[UITextField class] ] ) {
                searchBarTextField = (UITextField *)subSubView;
                searchBarTextField.delegate = self;
            }
        }
    }
}
if (searchBarTextField) {
    [searchBarTextField setReturnKeyType:UIReturnKeyNext];
}