0

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).

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

1 Answers1

1

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.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • What do you do before you get to this code? To prevent the error, wrap everything in `if (self.view.window) { /* code */ }` – nhgrif Aug 31 '14 at 19:22
  • So all of this code is in `viewDidLoad`? Move it to `viewWillAppear:` or `viewDidAppear:`. – nhgrif Aug 31 '14 at 19:27
  • A view doesn't have a window until it has been added as a subview to to a view which has a window, or, if it is a view controller's `.view` property, then it won't have a window in `viewDidLoad` (I think), it won't have a window until `viewWillAppear:` or `viewDidAppear:`. – nhgrif Aug 31 '14 at 19:31
  • Neither one gets executed... only -viewDidLoad does. – SpokaneDude Aug 31 '14 at 19:34
  • What do you mean neither gets executed? If `viewWillAppear:` or `viewDidAppear:` aren't executed than your view definitely won't have a window... because it's not appearing... and you can't show a popup on a view that's not appeared... which is what the runtime error is telling you. – nhgrif Aug 31 '14 at 19:35
  • Agreed... so how can I do this (display the results of the scan in a textbox in another class)? – SpokaneDude Aug 31 '14 at 19:38
  • Pass the results of the scan to that class. – nhgrif Aug 31 '14 at 19:40
  • How about if I take all of the "scan code" and put it in the class where I activate it (by user tapping button to do the scan)? I think that might work, so I'm gonna try it... thank you so much for your time, I really appreciate it. – SpokaneDude Aug 31 '14 at 19:42
  • That works... however, it stays on that "window"... how can I dismiss that window so my original UIView will show? (I already tried [self.tabBarController setSelectedIndex: 1], but that didn't do anything. – SpokaneDude Aug 31 '14 at 20:28
  • This is all very confusing. I recommend you look at some `UIPopoverController` and `UIPopoverDelegate` tutorials. – nhgrif Aug 31 '14 at 21:25