IBAction 不会在子视图中被调用

发布于 2024-10-22 18:05:01 字数 1015 浏览 1 评论 0原文

我正在创建一个简单的应用程序,让用户在单击按钮后观看电影。您看到的第一个屏幕是带有 2 个按钮的主菜单。其中一个将用户发送到一个子视图,我在 IBAction 方法中将其编码如下:

ViewConroller *nextView = [[ViewController alloc] initWithNibName:@"NextMenu" bundle nil];
[self.view addSubview:[nextView view]];
[nextView release];

这部分很好,只是子视图在前一个视图的底部显示了一个 20 px 的条。我环顾四周,没有像 这个对我有用。这甚至不是主要问题。在子视图上,每当我单击按钮时,应用程序就会爆炸。 IBAction 方法甚至在执行方法内的一行之前就中断了。在这里,在 goBack IBAction 方法中,我尝试关闭子视图并返回主菜单:

[self.view removeFromSuperview];

我的 XIB 文件中的连接是正确的,并且一切似乎都按顺序进行。这些按钮只是没有执行。我不确定这是否与添加子视图或什么有关。我在谷歌和这个网站上进行了大量搜索,但仍然找不到解决方案。

这是我遇到的错误的一个例子:

Terminating app due to uncaught exception 'NSInvalidArgumentException, reason: '-[__NSCFType goBack:]: unrecognized selector sent to instance 0x5640b70'

希望你们能帮助我,这真的很令人沮丧。我感觉我已经尝试了一切。感谢您抽出时间。如果我需要提供更多信息,请告诉我。再次感谢。

I am creating a simple app that let's the user view movies after clicking buttons. The first screen you see is the Main Menu with 2 buttons. One of them sends the user to a sub view which I coded as this in an IBAction method:

ViewConroller *nextView = [[ViewController alloc] initWithNibName:@"NextMenu" bundle nil];
[self.view addSubview:[nextView view]];
[nextView release];

This part is fine except that the sub view shows a 20 px bar across the bottom of the previous view. I looked around and none of the solutions like this one work for me. This isn't even the main problem. On the sub view, whenever I click a button, the app explodes. The IBAction methods break before even executing a line inside the method. Here, in the goBack IBAction method, I am trying to dismiss the subview and go back to the main menu:

[self.view removeFromSuperview];

My connections are correct in my XIB files and everything seems to be in order. The buttons just aren't executing. I'm not sure if it has to do with adding the sub view or what. I have done a lot of searching around on google and this website, but I still can't find a solution.

Here is an example of the error I'm getting:

Terminating app due to uncaught exception 'NSInvalidArgumentException, reason: '-[__NSCFType goBack:]: unrecognized selector sent to instance 0x5640b70'

Hopefully you guys can help me out, this is really quite frustrating. I feel like I've tried everything. Thanks for your time. Let me know if I need to provide any more information. Thanks again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

美人如玉 2024-10-29 18:05:01
ViewController *nextView = [[ViewController alloc] initWithNibName:@"NextMenu" bundle nil];

视图控制器已创建。

[self.view addSubview:[nextView view]];

self.view 添加了 nextView.view 的值,因为它是子视图并保留了它(注意它保留了 next.view 值而不是 nextView< /code> 本身)

[nextView release];

nextView 被释放并立即释放(因为它从未被任何人保留)

Button 正在尝试向释放的对象发送消息 ->行为未定义,在您的情况下,存在其他对象(或看起来像对象的东西),并且它不理解 goBack: 的含义。

这就是为什么你不应该用自己的导航发明一个轮子(特别是如果你是新手)。请改用 UINavigationController。

ViewController *nextView = [[ViewController alloc] initWithNibName:@"NextMenu" bundle nil];

View controller is created.

[self.view addSubview:[nextView view]];

self.view added value of nextView.view as it's subview and retained it (N.B. it retained next.view value not nextView itself)

[nextView release];

nextView is released and immediately deallocated (because it has never been retained by anybody)

Button is trying to send a message to deallocated object -> behavior is undefined, in your case some other object (or something that looks like an object) is there and it doesn't understand what goBack: means.

That's why you should not invent a wheel (especially if you are new to this) with your own navigation. Use UINavigationController instead.

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