ios - 使用 UIPopoverPresentationController 在 iPhone 上的 Popover 演示

标签 ios objective-c uipopovercontroller popover

大家好,我想为 iPhone 制作一个弹出框,所以这是我出错的代码,请建议我想创建如下图所示的内容,但我的内容覆盖了整个屏幕,我不想使用 segue。所以基本上我想要如果我单击按钮,应弹出调整大小的 View Controller 我该如何实现这一点请帮助。我已按照本教程进行操作。
https://www.youtube.com/watch?v=UQBbJQNEDA4
enter image description here

#import "ViewController.h"
#import "Popup.h"

@interface ViewController ()<UIPopoverPresentationControllerDelegate>
{
    Popup * popupController;
    UIPopoverPresentationController *popupPresentationController;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)popupOnClick:(id)sender {

    popupController=[self.storyboard instantiateViewControllerWithIdentifier:@"Popup"];
    popupController.modalPresentationStyle=UIModalPresentationPopover;
    popupPresentationController= [popupController popoverPresentationController];
    popupPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popupController.preferredContentSize=CGSizeMake(150, 300);
    popupPresentationController.delegate = self;

    [self presentViewController:popupController animated:YES completion:NULL];


    // in case we don't have a bar button as reference
    popupPresentationController.sourceView = _popupButton;
    popupPresentationController.sourceRect = _popupButton.frame;
}
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
    return UIModalPresentationNone;
}

@end

最佳答案

这是你的问题:

[self presentViewController:popupController animated:YES completion:nil];
popupPresentationController= [popupController popoverPresentationController];
popupPresentationController.delegate = self;

该代码的顺序错误。您必须设置 delegate在调用 presentViewController 之前.

关于ios - 使用 UIPopoverPresentationController 在 iPhone 上的 Popover 演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41367940/

相关文章:

SWRevealViewControllerSeguePushController 中的 iOS 抽屉和标题图像

ios - 将弹出框添加到当前导航 Controller 层次结构

ios - 从 iOS8 开始,我们应该为所有 iCloud 应用程序使用 CloudKit 吗?

iphone - 架构 armv6 的 undefined symbol ,更新到 iOS 4.3

ios - iTunesConnect : cannot transfer app because of "You must turn off TestFlight beta testing for the app that you want to transfer"

ios - 自动布局 VFL : align all trailing in an horizontal constraint

objective-c - 为渐变位置创建 CGFloat 数组

ios - 处理程序远程通知背景

iPad弹出问题

iphone - UIPopoverController:为什么我的弹出窗口没有出现在我想要的地方?