0

**** UPDATE** getting the following crash: [UploadViewController _viewForPresenting]: unrecognized selector sent to instance 0x7abe4c00**

The old way of using UIPopOvers was deprecated in iOS 8; so I tried to upgrade; unfortunately it's not working. I don't get the popover per se, just the top of the current view at the bottom (see image 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);
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

1 Answers1

1

Read the warning you're getting:

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

UIPopoverPresentationController's barButtonItem property is supposed to be a UIBarButtonItem. You're setting it to your view controller, which isn't a UIBarButtonItem. The result is that UIKit tries to send messages to your view controller that UIBarButtonItem would be able to accept, but since your view controller isn't a UIBarButtonItem, it doesn't know how to handle those messages, and you crash.

Also, fix this while you're at it:

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

This one's an easy fix; just make your UploadViewController class conform to the UIPopoverPresentationControllerDelegate protocol (and implement whatever methods you need to make it function properly as such).

Tl;dr: Don't assign objects to properties of incompatible types. Also: When the compiler gives you a warning, it's trying to help ya ;-)

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • @SpokaneDude We've probably got another, unrelated problem. What's the backtrace of the crash you're getting? – Charles Srstka Mar 17 '18 at 15:58
  • @SpokaneDude Try typing `bt` into lldb and post the output. – Charles Srstka Mar 17 '18 at 16:59
  • @SpokaneDude Seriously, just post the backtrace you get from typing `bt`, it might show us what the problem is. Also, your screenshot makes it look like an exception is being thrown; if you hit Continue in the debugger, it should get logged to the console, and that will probably tell us what's going on. – Charles Srstka Mar 17 '18 at 18:28
  • @SpokaneDude The answer is, once again, to read the error message. We have: `should have a non-nil sourceView or barButtonItem set before the presentation occurs.` So, make sure you have either `sourceView` or `barButtonItem` set to something non-`nil`. Probably all you need to do is point `sourceView` at the thing you want the popover to appear on top of. – Charles Srstka Mar 17 '18 at 19:49
  • @SpokaneDude Okay, I see this line in your updated code: `popController.barButtonItem = (UIBarButtonItem *)oShowHelp;` The question is: what is `oShowHelp`? I'm willing to wager that it's either `nil` or an object of some class other than `UIBarButtonItem` (in which case, just casting it won't change it—it'll just make the compiler warning go away so the app can crash when you run it). – Charles Srstka Mar 17 '18 at 21:42
  • @SpokaneDude I'm going to take a stab in the dark and assume that `oShowHelp` is actually a `UIButton` rather than a `UIBarButtonItem`. If this is the case, just assign it to `sourceView` instead of `barButtonItem`. `UIButton` is a subclass of `UIView`, so that should work; you should only have to deal with `barButtonItem` if you're trying to attach the popover to a toolbar item. – Charles Srstka Mar 17 '18 at 21:44
  • Well done! (Best 100 points I've ever spent!)... no crash, popup shows up, but the UIWebView isn't working for some reason... I'm using cnn.com which is secure, so I don't know what the problem is. I'm going to close this and reopen another one for the web view issue. Thanks so much... I might have figured it out myself, after a few weeks of trying everything else! – SpokaneDude Mar 17 '18 at 23:36
  • @SpokaneDude Glad to hear your problem is solved. For the future, remember: warnings are trying to help you! Don't ignore them, or shut them up with things like casts—you'll not only hurt the compiler's feelings, but possibly cause weird problems that are hard to find! ;-) – Charles Srstka Mar 18 '18 at 00:59