2013-05-16 24 views
0

我在我的MKMapView上有一個UISearchBar,用於搜索地圖註釋。我正在嘗試整合一條警報,讓用戶知道是否找不到匹配項。問題是,即使發現了匹配,並且當我點擊取消按鈕,警報恢復時,警報也會出現。有什麼建議麼?UIAlertView在搜索中找不到匹配項

- (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]; 
} 
+0

僅僅是我,還是第一個'for'循環不需要在那裏? – danypata

回答

1

在開始時添加一個BOOL稱爲annotationFound。找到註釋後,將annotationFound設置爲YES。將警報移出for循環,並根據annotationFound結尾輸入if