开发者

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


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


If you want to push a view controller with a navigation controller, then your initial view must be within the navigation controller to begin with.

In your case, your tab bar controller should probably be put in the navigation controller if it isn't already. Then you can call your showDetails method the way you have done above. If it is in the nav controller, maybe you can post the code where you do that and I can try and offer more assistance.

Here is how you might do something like this in your applicationDidFinishLaunchingWithOptions: method

FirstViewController *fvc = [[FirstViewController alloc] init];
SecondViewController *svc = [[SecondViewController alloc] init];

UITabBarController *tabcon = [[UITabBarController alloc] init];
tabcon.viewControllers = [NSArray arrayWithObjects:fvc,svc,nil];

UINavigationController *navcon = [[UINavigationController alloc] initWithRootViewController:tabcon];

[self.window addSubview:navcon.view];

Then when you want to push a detail view controller, you just call:

DetailViewController *dvc = [[DetailViewController alloc] init];
[self.navigationController pushViewController:dvc];

If you set your view controllers as properties, you can call self.viewControllerName, just make sure you are allocating memory for them as well. Embedding your tab bar controller in a navigation controller like this gives you all the window dressing (back buttons, title bar, etc.) for free and it will adapt to your different views as they come on screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜