【问题标题】:searchBarSearchButtonClicked not firingsearchBarSearchButtonClicked 未触发
【发布时间】:2013-04-29 03:23:40
【问题描述】:

我研究过这个问题。看起来这是一个常见问题,但我尝试过的任何方法都没有奏效。我对 iPhone 应用程序开发和 Objective C 非常陌生。此时我要做的就是在搜索栏字段中输入一个字符串,并在点击键盘上的搜索按钮时返回 NSLog() 中的值。

我的头文件是这样的:

@interface ListingMapViewController : UIViewController <UISearchBarDelegate> 

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@end

我的类文件中有这个:

#import "ListingMapViewController.h"

@interface ListingMapViewController ()


@end

@implementation ListingMapViewController


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
searchBar.delegate =self;

[self handleSearch:searchBar];
NSLog(@"User searched for %@", searchBar.text);

}

@end

当我单击“搜索”按钮时,没有任何输出。我错过了什么?谢谢。

【问题讨论】:

标签: uisearchbar uisearchbardelegate


【解决方案1】:

(在问题编辑中回答。转换为社区 wiki 答案。参见Question with no answers, but issue solved in the comments (or extended in chat)

OP 写道:

通过在视图加载时设置 searchBar 委托解决了这个问题。我想将其设置为 searchBarSearchButtonClicked 为时已晚。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.searchBar setDelegate:self];
 //self.searchBar.delegate = self; <-- also works
}

现在,当我单击键盘上的“搜索”按钮时,搜索栏字段中的文本将输出到 NSLog()。欢呼!迎接下一个挑战。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    相关资源
    最近更新 更多