从 iOS 4 SDK 上的 SuperView 中删除视图
我正在使用 iOS 4 SDK 开发 iPhone 3.1.3 应用程序。
我有两个 ViewController,mainViewController 和 AboutViewController。
我使用此代码从 mainViewController 转到 AboutViewController (mainViewController.m 内的代码):
- (IBAction) aboutClicked:(id)sender
{
AboutViewController* aboutController =
[[AboutViewController alloc]
initWithNibName:@"AboutViewController"
bundle:nil];
[self.view addSubview:aboutController.view];
[aboutController release];
}
这从 AboutViewController 返回到 mainViewController (AboutViewController.m 内的代码):
- (IBAction) backClicked:(id) sender
{
[self.view removeFromSuperview];
}
当我单击 AboutViewController 上的“后退”按钮时,我得到一个 EXC_BAD_ACCESS。
我正在使用基于窗口的应用程序模板。
我还尝试在 [self.view removeFromSuperview]
中添加断点,但我不能。
你知道为什么吗?
I'm developing an iPhone 3.1.3 app with iOS 4 SDK.
I have two ViewControllers, mainViewController and AboutViewController.
I use this code to go from mainViewController to AboutViewController (code inside mainViewController.m):
- (IBAction) aboutClicked:(id)sender
{
AboutViewController* aboutController =
[[AboutViewController alloc]
initWithNibName:@"AboutViewController"
bundle:nil];
[self.view addSubview:aboutController.view];
[aboutController release];
}
And this to come back from AboutViewController to mainViewController (code inside AboutViewController.m):
- (IBAction) backClicked:(id) sender
{
[self.view removeFromSuperview];
}
When I click on Back Button on AboutViewController, I get an EXC_BAD_ACCESS.
I'm using a window-based application template.
I've also tried to add a breakpoint in [self.view removeFromSuperview]
but I can't.
Do you know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这样做:然后
从 AboutViewController 返回到 mainViewController (AboutViewController.m 内的代码):
Do this instead:
And this to come back from AboutViewController to mainViewController (code inside AboutViewController.m):
你得到 EXC_BAD_ACCESS 的原因是因为在将 viewController 的视图添加为子视图后你释放了控制器,因此触摸事件无法看到预期的 viewController 来处理它。
注释掉如下的发布声明,它应该可以工作
The reason why you get EXC_BAD_ACCESS is because after adding the view of a viewController as sub view you released the controller, hence the touch event couldn't see the intended viewController to process it.
comment out the release statement like below and it should work
尝试:
呈现视图并:
删除视图...
Try:
To present the view and:
To remove the view...
1) 使 aboutController 成为类级别变量
2) 创建一个委托方法来处理
(IBAction) backClicked:(id) sender
3) 在委托调用的实现中
1) Make aboutController a class level variable
2) Create a delegate method to handle
(IBAction) backClicked:(id) sender
3) In implementation of delegate call