マップの注釈を検索する 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];
}