0

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

1 回答 1

1

BOOL在开头添加一个称为annotationFound. 找到注释后,设置annotationFound为 YES。将警报移出 for 循环,并if在最后基于annotationFound.

于 2013-05-16T20:24:21.010 回答