2015-10-15 65 views
0

我想在每個圖釘上顯示不同的註釋,圖像名爲「01.png」到「08.png」。 我設置整數隨機數從1〜8如何隨機顯示註釋

integer = arc4random() % 8 + 1; 

然後設置方法 - (MKAnnotationView *)的MapView:(的MKMapView *)的MapView viewForAnnotation:(ID)註釋

if(annotation == mapView.userLocation){ 
    return nil; 
} 
NSString *identifier = @"Store"; 

MKAnnotationView *result = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; 

if(result == nil){ 
    result = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; 
} else { 
    result.annotation = annotation; 
} 

NSString * imageName = [NSString stringWithFormat:@"0%i",integer]; 

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]]; 
result.image = image; 

return result; 

但的MapView顯示相同的註釋,它會在每次打開應用程序時顯示不同的註釋。

如何在每個引腳上顯示不同的註釋?

非常感謝!

回答

1

您必須將註釋方法內的隨機化。一個變量有一個值,直到它被改變,每次讀取它時都不會得到一個新的隨機值。

+0

它的工作原理!謝謝! – Chien