2013-10-30 31 views
2

我有一個搜索欄,它可以在用戶輸入時過濾聯繫人,但是我也需要直接訪問文本框,以防他們輸入的名字不在地址簿中。所以,我需要同時使用UISearchBarDelegate和UITextFieldDelegate。在iOS 7中,在UISearchBar中設置UITextField的委託似乎破壞了兩者。我怎樣才能讓兩位代表作出迴應?

在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中?謝謝。

+1

同樣的問題..請回復,如果有人發現soltuion。 – iEngineer

回答

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]; 
} 
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]; 
     } 
    } 
} 
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]; 
} 
相關問題