一个视图中的两个 UIPopover
我试图将两个不同的 UIPopovers 放在一个视图中。我对 Objective-C 和一般编程相当陌生,所以我没有采用一种聪明、有效的方法,即拥有一个弹出窗口并根据它的调用方式更改其内容,而是使用了一种愚蠢、简单的方法,即创建两个视图,两个代表,两个弹出窗口等...我不知道这是否是我遇到问题的原因,或者是否有其他原因。
所以问题就在这里。在弹出窗口出现的视图的 viewdidload
中,我有以下代码:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
optionsViewController =[[OptionsViewController alloc]init];
optionsViewController.delegate = self;
popoverController = [[UIPopoverController alloc] initWithContentViewController:optionsViewController];
popoverController.popoverContentSize = CGSizeMake(320, 216);
[popoverController setDelegate:self];
newCurrencyViewController =[[newCurrencyViewController alloc]init];
newCurrencyViewController.delegate = self;
newCurrencyPopoverController = [[UIPopoverController alloc] initWithContentViewController:newCurrencyViewController];
newCurrencyPopoverController.popoverContentSize = CGSizeMake(320, 216);
[newCurrencyPopoverController setDelegate:self];
}
显然 optionsViewController
是出现在弹出窗口 1 内的 vc(弹出窗口控制器称为“popoverController”), newCurrencyViewController
是出现在弹出窗口 2 内的 vc(弹出窗口控制器名为“newCurrencyPopoverController
”)。
每次加载视图时,应用程序都会崩溃并出现 SIGABRT
错误,并且控制台会显示:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] must not be called with `nil`.'
此外,该行还显示一条警告“未找到实例方法 -alloc(返回类型默认为 id)”
saysnewCurrencyViewController =[[newCurrencyViewController alloc]init];
我的第一个想法是我在某个地方拼错了文件名,因为我认为问题在于它没有找到名为 newCurrencyPopoverController 的文件,但我检查了所有内容,但找不到任何拼写错误或任何内容。有什么想法吗?
非常感谢!
卢克
I am trying to put two different UIPopovers in one view. I'm fairly new to objective-c, and programming in general, so instead of doing the smart, efficient method of having one popover and changing it's contents depending on how it is called, I just used the stupid, simple method of just creating two views, two delegates, two popovers etc... I don't know if that's why I'm having a problem, or if it's for some other reason.
So here's the problem. In the viewdidload
of the view where the popovers appear, I have this code:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
optionsViewController =[[OptionsViewController alloc]init];
optionsViewController.delegate = self;
popoverController = [[UIPopoverController alloc] initWithContentViewController:optionsViewController];
popoverController.popoverContentSize = CGSizeMake(320, 216);
[popoverController setDelegate:self];
newCurrencyViewController =[[newCurrencyViewController alloc]init];
newCurrencyViewController.delegate = self;
newCurrencyPopoverController = [[UIPopoverController alloc] initWithContentViewController:newCurrencyViewController];
newCurrencyPopoverController.popoverContentSize = CGSizeMake(320, 216);
[newCurrencyPopoverController setDelegate:self];
}
Obviously optionsViewController
is the vc that appears inside popover 1 (with popover controller called "popoverController"), and newCurrencyViewController
is the vc that appears inside popover 2 (with popover controller called "newCurrencyPopoverController
").
Every time the view loads, the app crashes with a SIGABRT
error, and the console says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] must not be called with `nil`.'
Also, there's a warning saying "instance method -alloc not found (return type defaults to id)" for the line that
saysnewCurrencyViewController =[[newCurrencyViewController alloc]init];
My first thought was that I had misspelled the name of a file somewhere, since I think the problem is that it isn't finding the file called newCurrencyPopoverController, but I've checked everything and can't find any misspellings or anything. Any ideas?
Thanks very much!
LUKE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在调用变量
newCurrencyViewController
的方法alloc
+init
,但您应该将它们调用到该变量的类!有 bug 的行:
该行的结果将为
newCurrencyViewController == nil
。当您尝试使用该视图初始化UIPopoverController
时,它会像您所描述的那样崩溃。如果变量
newCurrencyViewController
属于类,例如CurrencyViewController
那么您应该将该行替换为以下行:You are calling methods
alloc
+init
of your variablenewCurrencyViewController
but you should call them to the class of that variable!Line with bug:
The result of this line will be
newCurrencyViewController == nil
. And when you will try to initUIPopoverController
with that view it will crash as you described.If variable
newCurrencyViewController
is of class, for example,CurrencyViewController
then you should replace that line with this one:当您调用 newCurrenceViewController 时,您没有可以调用 alloc 和 init 的对象。
你可能想要更多类似的东西
Or w/e 你的自定义视图控制器的名称是
you dont have an object to call alloc and init on when you are calling newCurrenceViewController.
you probably want something more like
Or w/e the name of your custom view controller is