2

我有一个搜索栏,可以在用户键入时过滤联系人,但我还需要直接访问文本字段,以防他们键入不在通讯簿中的姓名。所以,我需要同时让 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 中使用它吗?谢谢。

4

3 回答 3

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];
        }
    }
}
于 2014-01-26T05:38:53.463 回答
0

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

在此处输入图像描述

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];
}
于 2014-01-14T11:24:27.123 回答
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];
}
于 2014-04-30T23:21:46.173 回答