从iOS 7.1开始,我无法读取文本,也无法设置UISearchBar的占位符。 text 属性始终返回 nil,并且搜索栏始终是空白的白色矩形。在 iOS 7.0 及更低版本中,我没有遇到这样的问题。 7.1 是否要求我以不同方式对待 UISearchBars 才能使 text
和 placeholder
正常工作?
- (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
属性,而我们的却干扰了它。
这是始终为您的类别方法/属性名称添加前缀的一个很好的理由。 :)
关于ios - 在 iOS 7.1 中,UISearchBar 的文本和占位符属性不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22517389/