作者:羊咩咩0_581 | 来源:互联网 | 2023-02-08 13:21
如何解决《在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];
}