Search code examples
iphonecocoa-touchuinavigationcontrollerpushviewcontroller

pushViewController issue


I've got a TabBar application and 2 views. One view is a MKMapView with annotations and callouts etc. All works well. Clicking on the 'UIButtonTypeDetailDisclosure' button in the callout my 'showDetails' function fires (NSLOG..). Now I want to push a DetailView in 'showDetails'. I'd have to either use pushViewController or presentModalViewController

#pragma mark - MKMap methods
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

    if([annotation isKindOfClass:[CustomPlacemark class]])
    {
        MKPinAnnotationView *newAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:[annotation title]];
        newAnnotation.pinColor = MKPinAnnotationColorGreen;
        newAnnotation.animatesDrop = YES; 
        newAnnotation.canShowCallout = YES;
        newAnnotation.enabled = YES;
        //newAnnotation.pinColor=MKPinAnnotationColorPurple;


        NSLog(@"Created annotation at: %f %f", ((CustomPlacemark*)annotation).coordinate.latitude, ((CustomPlacemark*)annotation).coordinate.longitude);

        [newAnnotation addObserver:self
                        forKeyPath:@"selected"
                           options:NSKeyValueObservingOptionNew
                           context:@"GMAP_ANNOTATION_SELECTED"];


        UIImageView *leftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftIconView.png"]];
        newAnnotation.leftCalloutAccessoryView = leftIconView;
        [leftIconView release];

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [rightButton addTarget:self
                        action:@selector(showDetails:)
              forControlEvents:UIControlEventTouchUpInside];
        newAnnotation.rightCalloutAccessoryView = rightButton;


        [newAnnotation autorelease];

        return newAnnotation;
    }

    return nil;
}


- (void) showDetails:(id)sender {

    NSLog(@"Annotation Click");

    [self.navigationController pushViewController:self.detailViewController animated:YES];
    // I want to push a new View here...


}

Unfortunately this doesn't do anything (No error / no action). I reckon it's because I don't have a navigationController to use pushViewController.

- (void) showDetails:(id)sender {

    NSLog(@"Annotation Click");

    [self.navigationController pushViewController:self.detailViewController animated:YES];
    // I want to push a new View here...


}

i am trying to create and push a DetailViewController in the navigationController when my 'UIButtonTypeDetailDisclosure' button in notification is clicked. but navigationController is nil Any idea what to add to my 'showDetails' function?

Much appreciated


Solution

  • You need to have a navigation controller before pushing views on it:

    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    
    // this line calls the viewDidLoad method of detailController
    detailController.view.hidden = NO;
    
    UINavigationController *navigationController = [[UINavigationController alloc] detailController];
    navigationController.navigationBarHidden = YES;
    [navigationController setView:detailController.view];
    
    [self.view addSubview:navigationController.view];
    [detailController release];
    [navigationController release];
    

    Be careful this will initialize navigationController again and again if you do this in your showDetails:. So you need to initialize your navigation controller in eg. viewDidLoad and then you will be able to push your detailController onto it