问题
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);
}
回答1:
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. :)
来源:https://stackoverflow.com/questions/22517389/in-ios-7-1-uisearchbars-text-and-placeholder-properties-dont-work-anymore