一个视图中的两个 UIPopover

发布于 2024-12-03 17:02:42 字数 1758 浏览 6 评论 0原文

我试图将两个不同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2024-12-10 17:02:42

您正在调用变量 newCurrencyViewController 的方法 alloc + init,但您应该将它们调用到该变量的类!

有 bug 的行:

newCurrencyViewController =[[newCurrencyViewController alloc]init];

该行的结果将为 newCurrencyViewController == nil。当您尝试使用该视图初始化 UIPopoverController 时,它会像您所描述的那样崩溃。

如果变量 newCurrencyViewController 属于类,例如 CurrencyViewController 那么您应该将该行替换为以下行:

newCurrencyViewController =[[CurrencyViewController alloc] init];

You are calling methods alloc + init of your variable newCurrencyViewController but you should call them to the class of that variable!

Line with bug:

newCurrencyViewController =[[newCurrencyViewController alloc]init];

The result of this line will be newCurrencyViewController == nil. And when you will try to init UIPopoverController 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:

newCurrencyViewController =[[CurrencyViewController alloc] init];
别忘他 2024-12-10 17:02:42

当您调用 newCurrenceViewController 时,您没有可以调用 alloc 和 init 的对象。

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];

    //Here is your problem---------------------------------------------
    newCurrencyViewController =[[newCurrencyViewController alloc]init];
    //-----------------------------------------------------------------

    newCurrencyViewController.delegate = self;
    newCurrencyPopoverController = [[UIPopoverController alloc] initWithContentViewController:newCurrencyViewController];
    newCurrencyPopoverController.popoverContentSize = CGSizeMake(320, 216);
    [newCurrencyPopoverController setDelegate:self];


}

你可能想要更多类似的东西

newCurrencyViewController = [[UICurrencyViewController alloc] init];

Or w/e 你的自定义视图控制器的名称是

you dont have an object to call alloc and init on when you are calling newCurrenceViewController.

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];

    //Here is your problem---------------------------------------------
    newCurrencyViewController =[[newCurrencyViewController alloc]init];
    //-----------------------------------------------------------------

    newCurrencyViewController.delegate = self;
    newCurrencyPopoverController = [[UIPopoverController alloc] initWithContentViewController:newCurrencyViewController];
    newCurrencyPopoverController.popoverContentSize = CGSizeMake(320, 216);
    [newCurrencyPopoverController setDelegate:self];


}

you probably want something more like

newCurrencyViewController = [[UICurrencyViewController alloc] init];

Or w/e the name of your custom view controller is

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文