使用 UIPopoverPresentationController 在 iPhone 上呈现 Popover

Popover presentation on an iPhone using UIPopoverPresentationController

大家好,我想为 iPhone 创建一个弹出窗口,所以这是我的代码,但我出错了,请建议我创建如下图所示的东西,但我的覆盖了整个屏幕,我不想使用 segue 。所以基本上我想要如果我点击按钮一个调整大小的视图控制器应该弹出我如何实现这个请 help.I 已经遵循了本教程。 https://www.youtube.com/watch?v=UQBbJQNEDA4

#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;

该代码的顺序错误。您必须在 调用 presentViewController.

之前设置 delegate