In iOS 7.1, UISearchBar's text and placeholder properties don't work anymore

烂漫一生 提交于 2019-12-25 04:25:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!