gpt4 book ai didi

ios - 如何在UIBarbutton中将UIAlertcontroller实现为PopOver(向上箭头方向)

转载 作者:行者123 更新时间:2023-12-01 18:45:02 24 4
gpt4 key购买 nike

我知道这是一个老派的问题-但我确实在网上搜索并发现不赞成使用的解决方案。我将如何在 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中对其进行测试。

最佳答案

提醒一下,因为这是Google的最佳结果:

popPresenter.barButtonItem是popPresenter.sourceView + popPresenter.sourceRect的替代方法

See (source)

关于OP问题,应使用IBAction参数sender

UIPopoverPresentationController *popPresenter = [alertController
popoverPresentationController];
popPresenter.barButtonItem = sender;
[self presentViewController:alertController animated:YES completion:nil];

关于ios - 如何在UIBarbutton中将UIAlertcontroller实现为PopOver(向上箭头方向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36830807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com