Search code examples
objective-cios7uipopovercontrolleruipopover

Why this build error when trying to attach a UIPopover to a UIButton in a different class?


I am trying to incorporate a bar code scanning api into my ipad app (XCode5, iOS7, Storyboards). I can't seem to get it to work, although the code is executed. For the sake of brevity, the complete scan code can be found here. The problem appears to be in this bit of code from the scan class:

 //  define the window
_highlightView = [[UIView alloc] init];
_highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
_highlightView.layer.borderColor = [UIColor greenColor].CGColor;
_highlightView.layer.borderWidth = 3;
[self.view addSubview:_highlightView];

What I think I need is a UIPopover with a small UIView in it because nothing appears on my current UIView. So this is the code I came up with:

UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 350, 500)];
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(350, 500);

_highlightView = [[UIView alloc] init];
_highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
_highlightView.layer.borderColor = [UIColor greenColor].CGColor;
_highlightView.layer.borderWidth = 3;
[self.view addSubview:_highlightView];

//  if previous popoverController is still visible... dismiss it
if ([popoverController isPopoverVisible]) {
    [popoverController dismissPopoverAnimated:YES];
}

//create a popover controller
DetailViewController *dvc = [[DetailViewController alloc]init];

popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

[popoverController presentPopoverFromRect:dvc.oReadBarCode.frame 
                               inView:self.view
             permittedArrowDirections:UIPopoverArrowDirectionUp 
                             animated:YES];

However, I'm getting a build error on the last line where I presentPopoverFromRect defining the button to attach the popover to:

Popovers cannot be presented from a view which does not have a window.'

How do I fix this? (The idea is to have a UIPopover display the results of the scan).


Solution

  • You don't need to cast the frame to a button... plus, your parenthesis seem to be off.

    You can simply do this:

    [popoverController presentPopoverFromRect:dvc.oReadBarCode.frame 
                                       inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionUp 
                                     animated:YES];
    

    The compiler complaint itself (for future reference) seems to be because you're trying to cast a struct to a pointer, which can't be done.