我在MKMapView上有一个UISearchBar,它可以搜索地图注释。我正在尝试合并一个警报,让用户知道是否未找到匹配项。问题是,即使找到匹配项,警报也会出现,而且当我单击“取消”按钮时,警报也会重新出现。有什么建议么?

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    id<MKAnnotation> ann;

    for (int i = 0; i < [marketLocations count]; i++)
    {
        for (ann in marketLocations)
        {
            NSString *annTitle = ann.title;
            NSString *annSubtitle = ann.subtitle;
            NSString *searchText = [searchBar text];
            NSRange titleRange = [annTitle rangeOfString:searchText options:NSCaseInsensitiveSearch];
            NSRange subtitleRange = [annSubtitle rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (titleRange.location != NSNotFound)
            {
                [worldView selectAnnotation:ann animated:YES];
            }
            else if (subtitleRange.location != NSNotFound)
            {
                [worldView selectAnnotation:ann animated:YES];
            }
            else if (titleRange.location == NSNotFound || subtitleRange.location == NSNotFound)
            {
                UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"" message:@"No Matches Found" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [av dismissWithClickedButtonIndex:0 animated:YES];
                [av show];
            }
        }
    }

    [searchBar resignFirstResponder];
}

最佳答案

在名为BOOL的开头添加annotationFound。找到注释后,将annotationFound设置为YES。将警报从for循环中移出,最后基于if移到annotationFound中。

关于ios - UIAlertView在搜索中找不到匹配项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16596722/

10-12 04:21