2

我想在我的 iOS 应用程序中添加一个谷歌即时搜索栏,就像 iOS Safari 中的那个一样,但我在任何地方都找不到它。你们能帮忙吗?提前致谢。

4

1 回答 1

2

我假设您正在使用 UISearchBar。

您可以使用您的代码

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar

在方法中

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

请记住,您不需要在searchBar:textDidChange 中放弃FirstResponder。

例如,在这个方法中可以是:

NSError *error = nil;

// We use an NSPredicate combined with the fetchedResultsController to perform the search
if (self.mySearchBar.text !=nil)
{
    if ([searchCriteria isEqualToString:@"artist"])
    {
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"artist  contains[cd] %@", self.mySearchBar.text];
        [fetchedResultsController.fetchRequest setPredicate:predicate];
    }
}
else
{
    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"All"];
    [fetchedResultsController.fetchRequest setPredicate:predicate];
}

if (![[self fetchedResultsController] performFetch:&error])
{
    // Handle error
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
}

// this array is just used to tell the table view how many rows to show
fetchedObjects = fetchedResultsController.fetchedObjects;

// dismiss the search keyboard
[mySearchBar resignFirstResponder];

// reload the table view
[myTableView reloadData];

}    

随意+1这个答案,如果这对像我这样的人有帮助,他们来自谷歌搜索结果到这篇文章:)

刷子51

于 2012-01-05T12:14:23.357 回答