我遇到以下代码的“问题”。错误消息是:应用程序尝试在单击注释#4的标注时在目标UINavigationController上推送nil视图控制器:0x10b82bbf0。 如何在不收到此错误的情况下打开警报视图?
//Callout button action
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control{
if ([view.annotation isKindOfClass:[Annotation class]])
{
Annotation *myAnn = (Annotation *)view.annotation;
id vcToPush = nil;
if ([[myAnn title] isEqualToString:@"1. Annotation one"]){
vcToPush = [[FirstViewController alloc]init];
}
if ([[myAnn title] isEqualToString:@"2. Annotation two"]){
vcToPush = [[SecondViewController alloc]init];
}
if ([[myAnn title] isEqualToString:@"3. Annotation three"]){
vcToPush = [[ThirdViewController alloc]init];
}
if ([[myAnn title] isEqualToString:@"4. Annotation four"]){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Fourth annotation" message:@"Message" delegate:nil cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
[self.navigationController pushViewController:vcToPush animated:YES];
}
}
//Custom callout
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)myAnn {
//Current location blue dot
if ([myAnn isKindOfClass:[MKUserLocation class]])
{
((MKUserLocation *)myAnn).title = @"My position";
return nil;
}//
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
if (!pinView) {
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:myAnn reuseIdentifier:@"pinView"];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
else
{
pinView.annotation = myAnn;
}
return pinView;
}
谢谢!
答案 0 :(得分:0)
在if块中
if ([[myAnn title] isEqualToString:@"4. Annotation four"]){
vcToPush
未初始化。因此,它具有调用方法之前的值(在这种情况下为nil
)。
我假设您只想在#4的情况下显示警报。在这种情况下,在if块中添加一个return:
if ([[myAnn title] isEqualToString:@"4. Annotation four"]){
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Fourth annotation" message:@"Message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return;
}