gpt4 book ai didi

ios - UIViewController 中的 UISearchBar

转载 作者:行者123 更新时间:2023-12-01 22:35:21 25 4
gpt4 key购买 nike

我正在尝试以编程方式将搜索栏添加到 UIView(不是 UITableView)。目前,我只是尝试正确设置代表,以便当我单击“输入”按钮时,会打印一条消息:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
NSLog(@"enter button clicked");
[searchBar resignFirstResponder];
}

我已经有一些代码适用于 UITableViewController我正在努力适应。它看起来像这样(我跳过了与当前讨论无关的所有代码部分):
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UITableViewController *resultsTableController;


- (void)viewDidLoad {
[super viewDidLoad];
self.resultsTableController = [[SearchResultsController alloc] init];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultsTableController];
self.searchController.searchResultsUpdater = self;
[self.searchController.searchBar sizeToFit];
self.searchController.delegate = self;
self.searchController.searchBar.delegate = self;
self.definesPresentationContext = YES;

self.tableView.tableHeaderView = self.searchController.searchBar;
}

现在唯一没有转置为 UIViewController 的行案例是最后一个
self.tableView.tableHeaderView = self.searchController.searchBar;

我的 UIViewController使用包含 UIView 的 xib 定义调用 myViewUISearchBar调用 searchBar .所以我试着做
self.myView = self.searchController.searchBar;

或者
self.searchBar = self.searchController.searchBar;

但在第一种情况下 searchBar甚至没有显示,在第二种情况下,委托(delegate) self似乎丢失了(当我单击输入时没有任何 react )。

我究竟做错了什么 ?

最佳答案

这是您以编程方式将 UISearchBar 添加到 UIViewController 的方式:

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenWidth, 40.0)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.searchBar.tintColor = kRunnerSearchBarTintColor;

if ([RunnerUtilities isIOS7orAbove]) {
self.searchBar.searchBarStyle = UISearchBarStyleProminent;
[self.searchBar setBarTintColor:[UIColor colorWithRed:185.0/255.0 green:195.0/255.0 blue:202.2/255.0 alpha:0]];
}

self.searchBar.delegate = self;
[self.view addSubview:self.searchBar];

好吧,这与从 XIB 文件中添加它没有什么不同。首先,搜索栏是作为 View Controller View 的 subview 添加的,应该连接到 ViewController 中定义的搜索栏导出。其次,UIViewController 必须设置为搜索栏的委托(delegate)。请确保您遵循这一点,这应该有效。

编辑::

如果您尝试在 UIViewController 中使用 UISearchController,请尝试以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any custom init from here...

// Create a UITableViewController to present search results since the actual view controller is not a subclass of UITableViewController in this case
UITableViewController *searchResultsController = [[UITableViewController alloc] init];

// Init UISearchController with the search results controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

// Link the search controller
self.searchController.searchResultsUpdater = self;

// This is obviously needed because the search bar will be contained in the navigation bar
self.searchController.hidesNavigationBarDuringPresentation = NO;

// Required (?) to set place a search bar in a navigation bar
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

// This is where you set the search bar in the navigation bar, instead of using table view's header ...
self.navigationItem.titleView = self.searchController.searchBar;

// To ensure search results controller is presented in the current view controller
self.definesPresentationContext = YES;

// Setting delegates and other stuff
searchResultsController.tableView.dataSource = self;
searchResultsController.tableView.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
}

关于ios - UIViewController 中的 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32539426/

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