How to add the info icon to the nav bar and implement the showInfoView method?

796 views Asked by At

I am attempting to add an info button to the top of the navigation bar. The info button directs the user to the information view. The following code adds the iPhone default info button to the navigation bar. It is placed inside RootViewController.m 's viewDidLoad function.

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];

Now, the showInfoView method needs to be implemented, and the following code is used:

- (IBAction)showInfoView:(id)sender {

// Create the root view controller for the navigation controller
// The new view controller configures a Cancel and Done button for the
// navigation bar.
InfoViewController *addController = [[InfoViewController alloc]
                                          initWithNibName:@"InfoView" bundle:nil];

// Configure the RecipeAddViewController. In this case, it reports any
// changes to a custom delegate object.
addController.delegate = self;

// Create the navigation controller and present it modally.

UINavigationController *navigationController = [[UINavigationController alloc]
                                                initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];

// The navigation controller is now owned by the current view controller
// and the root view controller is owned by the navigation controller,
// so both objects should be released to prevent over-retention.
[navigationController release];

[addController release];    

}

But when the program builds and runs, clicking the info button results in the program crashing.

Did anyone come across this problem before? I have searched extensively and I haven't seen any body with trouble implementing the showInfoView method.

1

There are 1 answers

1
Muhammad Hewedy On

Use this code:

UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
[infoButton addTarget:self action:@selector(showInfoView:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = infoButton;

The showInfoView: method should work fine provided that the nib file name is "InfoView.xib" not "InfoViewController.xib"