在iOS 7.1中,UISearchBar的文本和占位符属性不再起作用 [英] In iOS 7.1, UISearchBar's text and placeholder properties don't work anymore
问题描述
从iOS 7.1 开始,我无法阅读文本,也无法设置UISearchBar的占位符。 text属性始终返回nil,搜索栏始终为空白色矩形。在iOS 7.0 及以下,我没有遇到过这样的问题。 7.1是否要求我以不同的方式处理UISearchBars以使 text
和占位符
工作?
Starting from iOS 7.1, I have not been able to read the text nor set the placeholder of UISearchBar. The text property always returns nil and the search bar is always a blank white rectangle. In iOS 7.0 and below, I had no such problems. Does 7.1 require me to treat UISearchBars differently to make text
and placeholder
work?
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
self.searchBar.placeholder = @"placeholder doesn't work in 7.1";
[self.view addSubview:self.searchBar];
self.button = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 44.0, 100.0, 44.0)];
self.button.backgroundColor = [UIColor redColor];
[self.button setTitle:@"print text" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(printSearchText) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)printSearchText
{
NSLog(@"print nil here: %@", self.searchBar.text);
}
推荐答案
这可能是也可能不是与您的问题有关,但是我们在 UISearchBar
上定义的类别引起了类似的问题。我们添加了一个 textField
属性,出于一些我不会涉及的hacky原因。似乎Apple在iOS 7.1中添加了他们自己的内部 textField
属性,而我们正在干扰它。
This may or may not be related to your problem, but we had a similar problem caused by a category we'd defined on UISearchBar
. We'd added a textField
property for some hacky reasons I won't go into. It appears that Apple added their own internal textField
property in iOS 7.1 and ours was interfering with it.
这是总是为您的类别方法/属性名称添加前缀的充分理由。 :)
This is a good reason to always prefix your category method/property names. :)
这篇关于在iOS 7.1中,UISearchBar的文本和占位符属性不再起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!