userButtonPressed
gets called with the UIButton
as parameter, when the userUIBtn
gets clicked.
However, I would like to access the UIBarButtonItem
instead, in order to display a popover around.
Here is the generateToolbar()
method, which generates the user navigation button.
-(void) generateToolbar {
// Initialize the `ButtonFactory`
ButtonFactory *buttonFactory = [[ButtonFactory alloc] init];
// Generate some `UIButton(s)`
UIButton *userUIBtn = [buttonFactory createButtonWithButtonType:ButtonTypeUser];
userUIBtn.frame = CGRectMake(0,0,55,20);
// Add an action to the `UIButton`
[userUIBtn addTarget:self action:@selector(userButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
// Generate an `UIBarButtonItem` with the `UIButton` as basis view
UIBarButtonItem *userBtn = [[UIBarButtonItem alloc] initWithCustomView:userUIBtn];
// Add the `UIBarButtonItem` into the right side of the `navigationItem`
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: userBtn, nil];
}
Here is the userButtonPressed()
method, which gets triggered by pressing the user button and generates a popover around that button - It is not being called.
- (IBAction) userButtonPressed:(UIBarButtonItem*)sender {
// Initialize the `ButtonFactory`
ButtonFactory *buttonFactory = [[ButtonFactory alloc] init];
// Generate some `UIButton(s)`
UIButton *feedbackUIBtn = [buttonFactory createButtonWithButtonType:ButtonTypeFeedback iconVisibility:YES textVisibility:YES capitalization:NO iconSize:20.0 textSize:20.0];
[feedbackUIBtn addTarget:self action:@selector(feedbackBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
feedbackUIBtn.frame = CGRectMake(0,50,300,50);
// Create a `UIViewController` and add the `UIButtons` as subviews
UIViewController *viewController = [[UIViewController alloc] init];
[viewController.view addSubview:feedbackUIBtn];
// Present the `UIViewController`
viewController.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:viewController animated:YES completion:nil];
// Set up the `UIPopoverPresentationController`
UIPopoverPresentationController *popController = [viewController popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
popController.barButtonItem = sender;
}
My solution was to edit the action handler in two places, I changed the parameter from UIBarButtonItem
to UIButton
and changed popController.barButtonItem = sender;
to popController.sourceView = sender;
.
- (IBAction) userButtonPressed:(UIButton*)sender {
...
popController.sourceView = sender;
...
}