Search code examples
iphoneobjective-cuinavigationcontrollermodalviewcontroller

ModalViewController dismiss view with selector


I have a modal view controller that is presented from my main view, I am adding a "Done" button in the top right hand side of the view (navigationcontroller). However, I can't get the selector to call the right method. Here is the code I am using to set up the modal view:

    GraphView *graphView = [[[GraphView alloc] initWithNibName:@"GraphView" bundle:nil] autorelease];

//Set values in the graphView view
[graphView setInterest:interestRateSlider.value / 10];
[graphView setMonths: (loanTermSlider.value / 2.0) * 12]; // Years * 12 = months
[graphView setPrincipal:[principal intValue]];

//show the graph view as a modal navigation controller view
UINavigationController *graphNavigationController = [[UINavigationController alloc] initWithRootViewController:graphView];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] 
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                               target:graphView
                               action:@selector(dismissView:)];
graphView.navigationItem.rightBarButtonItem = doneButton;
[graphNavigationController.navigationItem.rightBarButtonItem setTitle:@"Done"];
[graphView.navigationItem setTitle:@"Graph"];
[self presentModalViewController:graphNavigationController animated:YES];
[graphNavigationController release];
[doneButton release];

Then in my graphView class I have the method:

 -(void) dismissView {
     [self dismissModalViewControllerAnimated: YES];
 }

However, when running the code I get unrecognized selector. The selector is trying to call the method on the UINavigationController variable. How can I get the selector to call the right method?

Thanks


Solution

  • You made a mistaken when calling the selector:

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] 
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                               target:graphView
                               action:@selector(dismissView:)];
    

    when dismissView is defined as

    -(void) dismissView;
    

    you should define dismissView as

    -(void) dismissView:(id)sender;
    

    or call it without colon

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] 
                               initWithBarButtonSystemItem:UIBarButtonSystemItemCancel 
                               target:graphView
                               action:@selector(dismissView)];