Add BarButtonItem to Navigation Controller in Popover

I am trying to add UIBarButtonItems to a navigation controller that appears as a popup. I cannot add buttons, and I am wondering if anyone can help me.

Here is the code that I still have:

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]; 
+4
source share
3 answers

The UINavigationController expects the button to be attached to the view controller for the currently displayed view (the button depends on each view when navigating using the UINavigationController). UIViewController has a navigationItem property in which you need to attach your button, usually in the viewDidLoad method of the displayed view controller.

In your class for aStudentsViewController, define the 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 can also do this by setting rightBarButtonItem to your aStudentsViewController outside the class, but I think you will have trouble determining when the navigationItem object is available. It would be something like this:

 aStudentsViewController.navigationItem.rightBarButtonItem = myButton; 

I don't think this will work until the popover forces everything to boot, but I'm not sure about that. The best way is to put it in the viewDidLoad of your aStudentsViewController object.

+4
source

just use viewDidLoad method

 self.navigationItem.rightBarButtonItem = self.editButtonItem; 

or something else - your button

hope this helps

0
source
  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 this helps you ..

0
source

Source: https://habr.com/ru/post/1307048/


All Articles