我试图在屏幕上的某个地方创建一个简单的弹出窗口,但由于某些原因,它总是崩溃。它不会给我任何错误(僵尸对象已启用)
UIViewController *viewController = [[UIViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
view.backgroundColor = [UIColor redColor];
viewController.view = view;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:viewController];
[popover presentPopoverFromRect:CGRectMake(0, 0, 100, 100) inView:self.view.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];我只是想在一个随机位置创建一个弹出窗口,这是不可能的吗?
编辑:我也尝试过这样
@property (nonatomic, retain) UIPopoverController *popover;
@synthesize popover = _popover;
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [UIColor redColor];
_popover = [[UIPopoverController alloc] initWithContentViewController:viewController];
_popover.delegate = self;
[_popover presentPopoverFromRect:CGRectMake(0, 0, 200, 200) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];EDIT2:这可能是相关的,我忘记提到了,但是这段代码正在另一个视图中被调用。
这可以很好地工作!
UIViewController *viewController = [[UIViewController alloc] init];
viewController.contentSizeForViewInPopover = CGSizeMake(200, 200);
viewController.view.backgroundColor = [UIColor redColor];
UIPopoverController *popver1 = [[UIPopoverController alloc] initWithContentViewController:viewController];
[popver1 presentPopoverFromRect:CGRectMake(250, 200, 200, 200) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
DocumentViewController *document = [[DocumentViewController alloc] initWithIssue:_readerModel.currentIssue];
[self.navigationController pushViewController:document animated:YES];当我在DocumentViewController中调用相同的代码时,它不起作用。
发布于 2012-08-30 17:40:05
嗯。在这里我看不到崩溃的原因(当我测试它时,代码运行良好)。
但:如果代码中的self.view.superview为nil,它将崩溃,并显示如下消息
[...] Popovers cannot be presented from a view which does not have a window [...]和:如果您想要正确地呈现popover,还需要进行更多的修复:
popover.
contentSizeForViewInPopover属性设置在viewController中,以管理rect UIPopoverArrowDirectionDown的实际大小为UIPopoverArrowDirectionAny (特别是在使用带有origin=(0,0)的rect时)。否则您将看不到popover.发布于 2012-08-30 18:06:07
您可以在弹出窗口之前检查窗口是否为空
if (self.view.window != nil)
另外,您是否在viewDidLoad或viewWillAppear中调用上述代码
您可以尝试使用viewDidAppear方法或didMoveToWindow方法中的相同代码吗
发布于 2012-08-30 17:07:22
我想你应该把popover定义为一个属性,而不是一个局部变量
https://stackoverflow.com/questions/12193291
复制相似问题