【问题标题】:iPhone SDK: Not able to access TextField From SearchBariPhone SDK:无法从 SearchBar 访问 TextField
【发布时间】:2014-09-05 06:29:50
【问题描述】:

我创建了一个带有取消按钮的自定义搜索栏。我想启用 取消 按钮 Everytime.but只有在我开始编辑搜索栏后才启用。 我写了以下代码。

#pragma mark - Search Bar Methods

-(void)addSearchBar{

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 65, 320, 44)];
    [self.view addSubview:self.searchBar];
    self.searchBar.showsCancelButton = YES;
    self.searchBar.delegate = self;

  for (UIView *searchBarSubview in [self.searchBar subviews]) {    
    if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {    
      @try {
        // set style of keyboard
        [(UITextField *)searchBarSubview setKeyboardAppearance:UIKeyboardAppearanceAlert];

        // always force return key to be enabled
        [(UITextField *)searchBarSubview setEnablesReturnKeyAutomatically:NO];
      }
      @catch (NSException * e) {        
        // ignore exception
      }
    }
  }



}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self.searchBar removeFromSuperview];
    [btnSearch setEnabled:TRUE];

}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    [self.searchBar resignFirstResponder];

}

但我无法从其子视图中获取 textField。 请帮忙。

【问题讨论】:

标签: ios iphone uisearchbar


【解决方案1】:

尝试使用此代码从搜索栏获取文本字段

for (UIView *subView in self.searchBar.subviews){
        for (UIView *searchView in subView.subviews){
            if ([searchView isKindOfClass:[UITextField class]])
                {

                [(UITextField *)searchView setKeyboardAppearance:UIKeyboardAppearanceAlert];
                [(UITextField *)searchView setEnablesReturnKeyAutomatically:NO];
                break;
                }
        }
    }

【讨论】:

  • 完美!!。工作,谢谢。
猜你喜欢
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 1970-01-01
  • 2015-07-28
  • 2010-12-02
  • 2018-07-06
相关资源
最近更新 更多