gpt4 book ai didi

ios - iPhone SDK : Not able to access TextField From SearchBar

转载 作者:行者123 更新时间:2023-11-29 02:41:47 26 4
gpt4 key购买 nike

我创建了一个带有“取消”按钮的自定义搜索栏。我想每次都启用 取消 按钮。但它仅在我开​​始编辑搜索栏后启用。我写了以下代码。

#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];

}

但是我无法从其 subview 中获取textField。请帮忙。

最佳答案

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

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

关于ios - iPhone SDK : Not able to access TextField From SearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25679819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com