热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在iOS9上进行演示之前,UIPopoverPresentationController应该设置一个非零的sourceView或barButtonItem

如何解决《在iOS9上进行演示之前,UIPopoverPresentationController应该设置一个非零的sourceView或barButtonItem》经验,为你挑选了1个好方法。

我正在尝试使用自定义UIPopoverPresentationController类来显示弹出窗口.但它崩溃了错误() should have a non-nil sourceView or barButtonItem set before the presentation occurs.下面是我发生崩溃的按钮单击代码.

- (IBAction)showPopup:(UIButton *)sender {
ViewController *cOntentViewController= [[ViewController alloc] init];

    contentViewController.preferredCOntentSize= CGSizeMake(200, 200);
    contentViewController.modalPresentatiOnStyle= UIModalPresentationPopover;
    myPopoverController *popOver = [[myPopoverController alloc]initWithPresentedViewController:contentViewController presentingViewController:self andTintColor:[UIColor lightGrayColor]];

    popOver.delegate = self;
    popOver.permittedArrowDirectiOns= UIPopoverArrowDirectionUp;
    popOver.sourceRect = sender.frame;
    popOver.sourceView = self.view;
    [self presentViewController:contentViewController animated: YES completion: nil];
}

下面是我的自定义UIPopoverPresentationController的样子示例

myPopoverController.h file

@interface myPopoverController : UIPopoverPresentationController

@property (readonly) UIColor *tintColor;


-(instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController andTintColor:(UIColor *)aTintColor;

@end


myPopoverController.m file

//Some code for UIPopoverBackgroundView

-(instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
{

    self = [self initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController andTintColor: [UIColor redColor]];

    return self;
}


-(instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController andTintColor:(UIColor *)aTintColor
{

    self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController];

    if (!self) {
        return nil;
    }

    [super setPopoverBackgroundViewClass: [myPopoverControllerBackgroundView class]];
    tintColor = aTintColor;


    return self;
}

我没有barbutton但是我正在设置sourceView.我在这里做错了吗?感谢您的帮助



1> 小智..:

您也可以创建这样的popover演示控制器,它可能会起作用

- (IBAction)showPopup:(UIButton *)sender {

ViewController *cOntentViewController= [[ViewController alloc] init];
    contentViewController.preferredCOntentSize= CGSizeMake(200, 200);
    contentViewController.modalPresentatiOnStyle= UIModalPresentationPopover;

UIPopoverPresentationController *popoverpresentatiOnController= contentViewController.popoverPresentationController;
    popoverpresentationController.delegate = self;
    popoverpresentationController.permittedArrowDirectiOns= UIPopoverArrowDirectionUp;
    popoverpresentationController.sourceRect = sender.bounds;
    popoverpresentationController.sourceView = sender;
    [self presentViewController:contentViewController animated: YES completion: nil];
}


推荐阅读
author-avatar
羊咩咩0_581
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有