如何解决如果我的搜索谓词返回nil,则尝试弹出UIAlert
|| 如果用户进行搜索并且谓词返回nil,我将尝试弹出一条警告“未找到记录”的警报。 我尝试了此操作,但从未收到警报。我只是得到一个空的tableview。- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text != nil){
NSPredicate *template = [NSPredicate predicateWithFormat:@\"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH\"];
NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@\"SEARCH\"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
[[fetchedResultsController fetchedObjects] count];
if (fetchedResultsController.fetchedObjects != nil) {
//do nothing
} else {
//display alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@\"No records match.\"
delegate:self cancelButtonTitle:@\"OK\" otherButtonTitles:nil];
[alert show];
[alert release];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@\"Unresolved error %@,%@\",error,[error userInfo]);
abort(); // Fail
}
[self.myTable reloadData];
[sBar resignFirstResponder];
}
解决方法
谓词没有
-count
。谓词是一个表达式,其结果为true
或false
。而已。
您可以向NSFetchedResultsController
索取其-fetchedObjects
(返回一个数组),然后向该数组索取其-count
,但是只有在调用-performFetch:
之后才能执行此操作。
, 哈哈!得到它了...
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text != nil) {
NSPredicate *template = [NSPredicate predicateWithFormat:@\"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH\"];
NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@\"SEARCH\"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@\"Unresolved error %@,%@\",error,[error userInfo]);
exit(-1); // Fail
}
if ([[fetchedResultsController fetchedObjects] count] != 0) {
[self.myTable reloadData];
[sBar resignFirstResponder];
}
else {
//display alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@\"No records match.\"
delegate:self cancelButtonTitle:@\"OK\" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。