我知道这是旧学校的问题 - 但我确实在网上搜索过并找到了弃用的解决方案。如何在 barButton 中将 UIAlertcontroller 实现为 popOver(向上箭头方向)。这是代码:
- (IBAction)eventSortingAction:(UIBarButtonItem *)sender {
UIAlertController * view= [UIAlertController
alertControllerWithTitle:@"My Title"
message:@"Select you Choice"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
[view dismissViewControllerAnimated:YES completion:nil];
}];
[view addAction:ok];
[view addAction:cancel];
[view setModalPresentationStyle:UIModalPresentationPopover];
view.modalInPopover = YES;
view.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
view.popoverPresentationController.delegate = self;
[self presentViewController:view animated:YES completion:nil];
UIView* senderView = [sender valueForKey:@"view"]; //HACK
UIPopoverPresentationController* popover = view.popoverPresentationController;
if (popover) {
popover.sourceView = senderView;
popover.sourceRect = senderView.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.barButtonItem = self.actionBarButton;
popover.delegate = self;
}}
显然我总是得到一个“popover = nil”。请帮忙!提前谢谢!
顺便说一句,这段代码不是我的,只是在Xcode中测试它。
答案 0 :(得分:2)
提醒一下,因为这是Google的最佳结果:
popPresenter.barButtonItem是popPresenter.sourceView + popPresenter.sourceRect的替代
关于OP问题,应使用IBAction
参数sender
。
UIPopoverPresentationController *popPresenter = [alertController
popoverPresentationController];
popPresenter.barButtonItem = sender;
[self presentViewController:alertController animated:YES completion:nil];
答案 1 :(得分:0)
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *actnCamera = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
UIAlertAction *actnLibrary = [UIAlertAction actionWithTitle:@"Library" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
[alertController addAction:actnLibrary];
[alertController addAction:actnCamera];
[alertController setModalPresentationStyle:UIModalPresentationPopover];
UIPopoverPresentationController *popPresenter = [alertController
popoverPresentationController];
popPresenter.sourceView = self.view;
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.x = self.navigationItem.leftBarButtonItem.width;
popPresenter.sourceRect = frame;
popPresenter.barButtonItem = self.navigationItem.leftBarButtonItem;
[self presentViewController:alertController animated:YES completion:nil];
输出