views:

443

answers:

2

Hi everyone,

I'm trying to add UIBarButtonItems to a Navigation Controller that is displayed as a popup. I can't seem to add the buttons, and I'm wondering if someone can help me out.

Here is the code I have so far:

UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:aStudentsViewController];

[navigationController setToolbarHidden:NO];
[navigationController setNavigationBarHidden:NO];

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"All Present"
                                                            style:UIBarButtonItemStylePlain
                                                           target:self
                                                           action:@selector(makeAllPresent:)];  



[navigationController.navigationItem setRightBarButtonItem:myButton];

attendancePopoverController = [[UIPopoverController alloc] initWithContentViewController:navigationController];
[attendancePopoverController setDelegate:self];


//activeBarButtonItem = sender;
[attendancePopoverController presentPopoverFromBarButtonItem:attendanceButton
                                permittedArrowDirections:UIPopoverArrowDirectionAny
                                                animated:YES];
A: 

just use in the viewDidLoad method

self.navigationItem.rightBarButtonItem = self.editButtonItem;

or whatever is your button

hope it helps

the1nz4ne
A: 

UINavigationController expects the button to be attached to the view controller for the view it's currently displaying (the button is specific to each view as you navigate using a UINavigationController). UIViewController has a property for navigationItem which is where you need to attach your button, usually in the viewDidLoad method of the view controller being displayed.

In your class for aStudentsViewController define a viewDidLoad method and set the button there:

- (void)viewDidLoad {
    UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"All Present"
                                                         style:UIBarButtonItemStylePlain
                                                         target:self
                                                         action:@selector(makeAllPresent:)];  
    self.navigationItem.rightBarButtonItem = myButton;
}

You might also be able to do it by setting the rightBarButtonItem on your aStudentsViewController from outside the class, but I think you'll have trouble determining when the navigationItem object is available. It'd be something like this though:

aStudentsViewController.navigationItem.rightBarButtonItem = myButton;

I don't think it'd work until after the popover has caused everything to load, but I'm not really sure of that. The best way is to put it in the viewDidLoad of your aStudentsViewController object.

John Stephen