userButtonPressed
时,以UIButton
为参数调用 userUIBtn
。
但是,我想访问UIBarButtonItem
,以便显示一个popover。
以下是generateToolbar()
方法,可生成用户导航按钮。
-(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];
}
以下是userButtonPressed()
方法,按用户按钮触发,并在该按钮周围生成一个弹出窗口 - 它未被调用。< / p>
- (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;
}
答案 0 :(得分:1)
我的解决方案是在两个地方编辑动作处理程序,我将参数从UIBarButtonItem
更改为UIButton
并将popController.barButtonItem = sender;
更改为popController.sourceView = sender;
。
- (IBAction) userButtonPressed:(UIButton*)sender {
...
popController.sourceView = sender;
...
}