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