Objective c 为什么在尝试将UIPopover附加到其他类中的UIButton时会出现此生成错误?
我正在尝试将条形码扫描api整合到我的ipad应用程序(XCode5、iOS7、故事板)中。虽然代码已经执行,但我似乎无法让它工作。为了简洁起见,可以找到完整的扫描代码。问题似乎出在扫描类的这段代码中:Objective c 为什么在尝试将UIPopover附加到其他类中的UIButton时会出现此生成错误?,objective-c,ios7,uipopovercontroller,uipopover,Objective C,Ios7,Uipopovercontroller,Uipopover,我正在尝试将条形码扫描api整合到我的ipad应用程序(XCode5、iOS7、故事板)中。虽然代码已经执行,但我似乎无法让它工作。为了简洁起见,可以找到完整的扫描代码。问题似乎出在扫描类的这段代码中: // define the window _highlightView = [[UIView alloc] init]; _highlightView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresiz
// 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];
我想我需要的是一个带有小UIView的UIPover,因为我当前的UIView上没有显示任何内容。这就是我想出的代码:
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];
但是,在最后一行中,我出现了一个生成错误,在该行中,我显示了PopoverFromRect,它定义了要将popover附加到的按钮:
无法从没有窗口的视图中显示弹出窗口。”
我该如何解决这个问题?(想法是让UIPopover显示扫描结果)。您不需要将帧强制转换为按钮。。。另外,你的括号好像错了 您可以简单地执行以下操作:
[popoverController presentPopoverFromRect:dvc.oReadBarCode.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
编译器的抱怨本身(供将来参考)似乎是因为您试图将结构强制转换为指针,而这是无法完成的。在处理此代码之前,您要做什么?为了防止出现错误,请将所有代码都包装在
if(self.view.window){/*code*/}
中,以便所有这些代码都在viewdiload
中?将其移动到视图将显示:
或视图显示:
。视图只有作为子视图添加到具有窗口的视图中时才具有窗口,或者,如果它是视图控制器的.view
属性,则它在视图加载
中不具有窗口(我认为),直到视图出现:
或视图显示:
时,它才会有窗口。这两个窗口都不会被执行。。。只有-viewDidLoad可以。什么意思都不执行?如果视图将出现:
或视图显示:
未执行,则视图肯定不会有窗口。。。因为它没有出现。。。你不能在没有出现的视图上显示弹出窗口。。。这就是运行时错误告诉您的。