UISearchBar取消按钮Xcode 4.3?

时间:2012-03-04 06:35:23

标签: iphone uisearchbar xcode4.3 cancel-button

如何在用户开始在UISearchBar中输入文本时才显示取消按钮?

我试过这个

- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
self.searchBar.showsCancelButton = YES;
}

但它不起作用,我认为将它设置为“showsCancelButton”没有任何问题,因为即使我说

NSLog(@"Typing");

它不会打印到屏幕上。 Xcode 4.3中有另一种方法可以做到这一点吗? 是否还有一种方法可以知道是否按下了取消按钮?

3 个答案:

答案 0 :(得分:1)

您确定该课程是searchBar的代表吗?尝试在self.searchBar.delegate=self;方法中设置viewDidLoad

答案 1 :(得分:1)

您的方法未返回BOOLean值,“YES”。这应该有效:

// show search bar's cancel button when editing
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    searchBar.showsCancelButton = YES;

    return YES;
}
// hide search bar's cancel button when not editing
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {

    searchBar.showsCancelButton = NO;

    return YES;
}

答案 2 :(得分:0)

试试这个:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar {
  aSearchBar.showsCancelButton = YES;
}