iPhone上的UIPopoverPresentationController不会产生弹出窗口 [英] UIPopoverPresentationController on iPhone doesn't produce popover
问题描述
我正在尝试在我的iPhone应用程序中实现新的 UIPopoverPresentationController
(使用Objective C)。我想要的是一个简单的popover,其中包含一个从启动按钮发出的tableview。
I'm trying to implement the new UIPopoverPresentationController
in my iPhone app (using Objective C). What I want is a simple popover with a tableview that emanates from the initiating button.
- 编辑 -
这是我的修改后的代码,改编自文档中的研究,SO以及下面评论中的输入:
Here's my REVISED code, adapted from research in the docs, SO, and from input in comments below:
- (IBAction)selectCategoryBtn:(UIButton *)sender
{
[self performSegueWithIdentifier:@"CatSelectSegue" sender:self.selCatButton];
}
-(void) prepareForSegue:(UIStoryboardSegue *) segue Sender:(id) sender
{
if (sender == self.selCatButton)
{
if ([segue.identifier isEqualToString:@"CatSelectSegue"])
{
UIPopoverPresentationController *controller = segue.destinationViewController;
controller.delegate = self;
controller.sourceView = self.selCatButton;
controller.sourceRect = self.selCatButton.frame;
}
}
}
-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
return UIModalPresentationNone;
这是我的故事板连接:
< img src =https://i.stack.imgur.com/Hhz6g.pngalt =在此处输入图像描述>
然而,这只是一个tableview以模态的方式,从底部上升并消耗整个屏幕。
However this simply presents a tableview in a modal fashion, rising up from the bottom and consuming the entire screen.
我用谷歌搜索,看起来到处都是,但似乎我不是唯一让我感到困惑的是,我希望这会解决iPhone的棘手问题。
I've googled, and looked all over SO, but it appears that I'm not the only one confused by what I'd hoped would resolve a nettlesome issue for the iPhone.
任何人都可以在我的代码中看到一个小故障或指导我一个明确的教程吗?我看过了,但也许API就是这么新,没有人能够处理它。
Can anyone see a glitch in my code or direct me to a clear tutorial? I've looked, but maybe the API is just so new nobody's got a handle on it yet.
谢谢!
第二次修改:
以下是上述代码的结果。我缩小了视图控制器中tableview的大小,我希望将其作为弹出窗口呈现。我把背景涂成灰色,只是为了澄清出现的是什么,而不是弹出窗口。
Here's what gets presented as a result of the code above. I reduced the size of the tableview in the View Controller I expected to be presented as a popover. I colored the background gray, just to clarify what's showing up instead of the popover.
推荐答案
步骤:
A)使用 Present As Popover
segue类型将 UIButton
链接到popover的视图控制器。我实际上必须创建一个新项目才能显示它,但它可能与基本SDK有关。
A) Link your UIButton
to the popover's view controller using the Present As Popover
segue type. I actually had to create a new project to get this to appear but it's probably something to do with the base SDK.
B)使视图控制器包含 UIButton
符合< UIPopoverPresentationControllerDelegate>
。例如。在你的 MyViewController.m
文件中添加:
B) Make the View Controller containing the UIButton
conform to the <UIPopoverPresentationControllerDelegate>
. E.g. In your MyViewController.m
file add:
@interface MyViewController () <UIPopoverPresentationControllerDelegate>
C)将以下方法添加到包含 UIButton的View Controller
:
C) Add the method below to the View Controller containing the UIButton
:
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
D)将以下内容添加到 prepareForSegue:sender中:
替换 segue.identifier
检查:
D) Add the following into your prepareForSegue:sender:
replacing your segue.identifier
check:
if ([segue.identifier isEqualToString:@"CatSelectSegue"]) {
UIViewController *dvc = segue.destinationViewController;
UIPopoverPresentationController *controller = dvc.popoverPresentationController;
if (controller) {
controller.delegate = self;
}
}
代码测试并证明其有效:
Code tested and proof it works:
编辑:我的测试应用程序TPOPViewController.m文件,其中发生了魔术:
My test app TPOPViewController.m file where the magic happens:
#import "TPOPViewController.h"
@interface TPOPViewController () <UIPopoverPresentationControllerDelegate>//, UIAdaptivePresentationControllerDelegate>
@end
@implementation TPOPViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSString *identifier = segue.identifier;
if ([identifier isEqualToString:@"popover"]) {
UIViewController *dvc = segue.destinationViewController;
UIPopoverPresentationController *ppc = dvc.popoverPresentationController;
if (ppc) {
if ([sender isKindOfClass:[UIButton class]]) { // Assumes the popover is being triggered by a UIButton
ppc.sourceView = (UIButton *)sender;
ppc.sourceRect = [(UIButton *)sender bounds];
}
ppc.delegate = self;
}
}
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
return UIModalPresentationNone;
}
@end
我的测试故事板:
这篇关于iPhone上的UIPopoverPresentationController不会产生弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!