gpt4 book ai didi

ios - 抓取 UISearchBar 数据 - Objective C

转载 作者:行者123 更新时间:2023-11-29 03:03:26 27 4
gpt4 key购买 nike

我在我的一个 ViewControllers 中创建了一个 UISearchBar,它包含链接到其他 ViewControllers 的不同 containers >。此 UISearchBar 位于父级 ViewController 中。我试图获取在搜索栏中输入的文本,用于发送到另一个 ViewController UISearchBar 是在 StoryBoard 中创建的。我正在使用以下方法尝试从搜索栏接收信息:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
NSLog(@"searching");
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"Text change");

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
}

上面的这些功能似乎都没有运行,我不确定为什么。建议或想法?

更新:

这是我现在用来启动搜索栏的内容:

_aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[_aSearchBar sizeToFit];
_aSearchBar.delegate = self;
_aSearchBar.placeholder = @"Search YouTube...";
_searchDC = [[UISearchDisplayController alloc] initWithSearchBar:_aSearchBar contentsController:self];

[self performSelector:@selector(setSearchDisplayController:) withObject:_searchDC];

_searchDC.delegate = self;
_searchDC.searchResultsDataSource = self;
_searchDC.searchResultsDelegate = self;
[_aSearchBar release];
NSLog(@"%@",[_searchDC.delegate class]); <------ this prints home which is the correct class

现在唯一的问题是,我再也看不到搜索栏了吗?

最佳答案

如果未调用委托(delegate)方法,则意味着 UISearchBar 不知道将 UISearchBarDelegate 协议(protocol)中的可选方法委托(delegate)到何处。

所以实现这些方法的ViewController

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
NSLog(@"searching");
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"Text change");

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
}

必须设置为 UISearchBar 的委托(delegate)。为了确保将正确的 ViewController 设置为 UISearchBar 的委托(delegate),我将打印

[searchBar.delegate 类]

如果这返回 nil 或其他一些类名,然后是您期望的类名,您就会知道问题出在哪里。

希望这对您有所帮助。

更新

好的,我刚刚创建了一个单 View 应用程序并在 Storyboard上拖了一个 UISearchBar。然后我为 UISearchBar 添加了一个名为 searchBar 的导出。然后我让 ViewController 实现 UISearchBarDelegate 协议(protocol)。然后在 ViewDidLoad 中设置 searchBar.delegate = self。在我添加上面列出的所有委托(delegate)方法后,一切正常......所以我现在唯一能想到的是 ViewController 没有实现协议(protocol)

@interface ViewController () <UISearchBarDelegate>

这是 ViewController 源代码:

#import "ViewController.h"

@interface ViewController () <UISearchBarDelegate>

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

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.searchBar.delegate = self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
NSLog(@"searching");
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSLog(@"Text change");

}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Cancel clicked");
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"Search Clicked");
}

关于ios - 抓取 UISearchBar 数据 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23052171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com