如果我的搜索谓词返回 nil,则尝试弹出 UIAlert

发布于 2024-11-16 04:32:41 字数 1408 浏览 7 评论 0原文

如果用户进行搜索并且谓词返回 nil,我会尝试弹出一条警告,提示“未找到记录”。

我尝试过这个,但警报从未被调用。我只是得到一个空的桌面视图。

- (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];  

}

I'm trying to pop an alert that says "No records found" if the user does a search and the predicate returns nil.

I tried this, but the alert never gets called. I just get an empty 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];  

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

于我来说 2024-11-23 04:32:41

谓词没有 -count。谓词 是一个计算结果为 truefalse 的表达式。就是这样。

您可以向 NSFetchedResultsController 询问其 -fetchedObjects (返回一个数组),然后向该数组询问其 -count,但您可以仅在调用 -performFetch: 后执行此操作。

A predicate does not have a -count. A predicate is an expression that evaluates to true or false. That's it.

You can ask the NSFetchedResultsController for its -fetchedObjects (which returns an array), and then ask that array for its -count, but you can only do this after you invoke -performFetch:.

無處可尋 2024-11-23 04:32:41

哈哈!知道了...

- (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];

    }   
}

Ha ha! Got it...

- (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];

    }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文