UIAlertView搜索中找不到匹配项

时间:2013-05-16 20:19:37

标签: ios uialertview uisearchbar

我的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];
}

1 个答案:

答案 0 :(得分:1)

在名为BOOL的开头添加annotationFound。找到注释后,将annotationFound设置为YES。根据{{​​1}}将警报移出for循环,最后移至if