处理 UIPopover 的旧方法不起作用;复制的代码,现在崩溃了

Old method of handling UIPopovers not working; copied code, now getting crash

**** 更新** 发生以下崩溃:[UploadViewController _viewForPresenting]:无法识别的选择器发送到实例 0x7abe4c00**

在 iOS 8 中弃用了使用 UIPopOvers 的旧方法;所以我尝试升级;不幸的是它不工作。我没有得到弹出窗口本身,只有底部当前视图的顶部(见图 here). I'm sure something is missing here, but after spending 2 days on it, I don't see the problem. I've been away from the coding effort, so I am requesting help in solving this coding issue. This is my code (copied and modified from here):

    //  make the popover
UIViewController * popoverContent = [UIViewController new];

UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 614, 804)];
popoverView.backgroundColor = [UIColor colorWithWhite:(CGFloat)1.0 alpha:(CGFloat)1.0];  //  frame color?
popoverContent.view = popoverView;

//resize the popover view shown in the current view to the view's size
popoverContent.preferredContentSize = CGSizeMake(614, 804);

// NSString *urlAddress = @"https://google.com";
NSString *urlAddress = @"http://pragerphoneapps.com/upload-help/";
NSURL  *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

//  add the UIWebView for RichText
UIWebView *webView = [[UIWebView alloc] initWithFrame: popoverView.frame];
webView.backgroundColor = [UIColor whiteColor];  //  change background color here

//  add the webView to the popover
[webView loadRequest:requestObj];
[popoverView addSubview:webView];

//create a popover controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"UploadViewController"];

//  present the controller
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController: controller animated:YES completion:nil];

//  configure the Popover presentation controller
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionUp; 

//  get warning on the following line: Incompatible pointer types assigning to 'UIBarButtonItem * _Nullable' from 'UploadViewController *'
popController.barButtonItem = self;

//  also get warning on the following line:  Assigning to 'id<UIPopoverPresentationControllerDelegate> _Nullable' from incompatible type 'UploadViewController *const __strong'  
popController.delegate = self;

popController.sourceView = popoverView;
popController.sourceRect = CGRectMake(10, 10, 614, 804);

阅读您收到的警告:

//  get warning on the following line: Incompatible pointer types assigning to 'UIBarButtonItem * _Nullable' from 'UploadViewController *'
popController.barButtonItem = self;

UIPopoverPresentationControllerbarButtonItem 属性 应该是 UIBarButtonItem。您将其设置为您的视图控制器,它不是 UIBarButtonItem。结果是 UIKit 尝试将 UIBarButtonItem 能够接受的消息发送到您的视图控制器,但是由于您的视图控制器不是 UIBarButtonItem,它不知道如何处理这些消息,然后你崩溃了。

另外,边做边解决这个问题:

//  also get warning on the following line:  Assigning to 'id<UIPopoverPresentationControllerDelegate> _Nullable' from incompatible type 'UploadViewController *const __strong'  
popController.delegate = self;

这个很容易修复;只需让您的 UploadViewController class 符合 UIPopoverPresentationControllerDelegate 协议(并实施使其正常运行所需的任何方法)。

Tl;dr:不要将对象分配给不兼容类型的属性。另外:当编译器给你一个警告时,它试图帮助你 ;-)