IBAction 不会在子视图中被调用
我正在创建一个简单的应用程序,让用户在单击按钮后观看电影。您看到的第一个屏幕是带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
视图控制器已创建。
self.view
添加了nextView.view
的值,因为它是子视图并保留了它(注意它保留了next.view
值而不是nextView< /code> 本身)
nextView
被释放并立即释放(因为它从未被任何人保留)Button 正在尝试向释放的对象发送消息 ->行为未定义,在您的情况下,存在其他对象(或看起来像对象的东西),并且它不理解
goBack:
的含义。这就是为什么你不应该用自己的导航发明一个轮子(特别是如果你是新手)。请改用 UINavigationController。
View controller is created.
self.view
added value ofnextView.view
as it's subview and retained it (N.B. it retainednext.view
value notnextView
itself)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.