ios - 在 iOS 7.1 中,UISearchBar 的文本和占位符属性不再起作用

标签 ios iphone cocoa-touch ios7

从iOS 7.1开始,我无法读取文本,也无法设置UISearchBar的占位符。 text 属性始终返回 nil,并且搜索栏始终是空白的白色矩形。在 iOS 7.0 及更低版本中,我没有遇到这样的问题。 7.1 是否要求我以不同方式对待 UISearchBars 才能使 textplaceholder 正常工作?

- (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/

相关文章:

ios - 无法在 tableViewCell 中设置横向坐标

ios - 在AlertView中选择“取消”按钮时,如何从阵列中消除所有AlertView?

objective-c - 如何制作一个小的透明模态整体来表示加载数据?

ios - UITableView 的实现,遵循 MVC 模式

iphone - 从服务器加载 "weird characters"的标签

ios - 多次加载同一屏幕时 firebase 快照发生变化

ios - 函数中缺少返回 - 模型中出现快速错误

iphone - CGContextClearRect 带圆圈

iphone - 使用我的应用程序打开文件

objective-c - 设置遵循椭圆路径的 CAKeyframeAnimation 的开始位置