开发者

Add BarButtonItem to Navigation Controller in Popover

开发者 https://www.devze.com 2022-12-27 05:07 出处:网络
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.

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];


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.


just use in the viewDidLoad method

self.navigationItem.rightBarButtonItem = self.editButtonItem;

or whatever is your button

hope it helps


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

     UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
     vc.navigationItem.title = @"Your Title";
     vc.navigationItem.rightBarButtonItem =myButton;

     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
    navigationController.navigationBar.hidden = NO;

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

Hope it helps ya..

0

精彩评论

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