0

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

旧的使用 UIPopOvers 的方式在 iOS 8 中被弃用了;所以我尝试升级;不幸的是,它不起作用。我没有得到弹出框本身,只是底部当前视图的顶部(参见此处的图片)。我确定这里缺少一些东西,但是在花了 2 天之后,我没有看到问题。我已经远离编码工作,所以我请求帮助解决这个编码问题。这是我的代码(从这里复制和修改):

    //  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);
4

1 回答 1

1

阅读您收到的警告:

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

UIPopoverPresentationControllerbarButtonItem属性应该是一个UIBarButtonItem. 您将其设置为您的视图控制器,它不是UIBarButtonItem. 结果是 UIKit 尝试向您的视图控制器发送UIBarButtonItem可以接受的消息,但是由于您的视图控制器不是 a UIBarButtonItem,它不知道如何处理这些消息,并且您崩溃了。

另外,在您使用它时解决此问题:

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

这个很容易解决;只需使您的UploadViewController类符合UIPopoverPresentationControllerDelegate协议(并实现使其正常运行所需的任何方法)。

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

于 2018-03-17T14:12:26.253 回答