问题描述
代码会因为循环引用而崩溃吗?
Does the code crash, because of a circular reference?
MenuController: UIViewController
- (id)initWithNibName:
{...
TabsController *tabs = [[TabsController alloc] initWithNibName:@"TabsController" bundle:nil];
self.tab = tabs;
....
}
//button pressed:
- (IBAction)showPrefFromMenu:(id)sender {
// todo change delegate!?
tab.tabDelegate = self;
[self presentModalViewController:tab animated:YES];
//[tab release];
}
// delegate method:
-(void)myViewDismissed {
....
NSLog(@"tab references: %d", [tab retainCount]) ;
[self dismissModalViewControllerAnimated:YES];//crash
...
}
模态/子类:
TabsController : UIViewController <...>
- (IBAction)dismissTabs:(id)sender {
...
NSLog(@"dismissTabs: sender: %@",sender);
[self.tabDelegate myViewDismissed];
}
我看到 self.tabDelegate 是 MenuController 实例,并且在该代码上想要关闭并释放 TabsController.
As I see the self.tabDelegate is the MenuController instance and on that code want do dismiss and deallocate the TabsController.
虽然在 [self.tabDelegate myViewDismissed] 之后不再是代码;但是如果它不能执行,因为它被释放了,也许程序集 Ret 或者什么指令不能执行?返回语句.
Although it isn't any more code after [self.tabDelegate myViewDismissed]; but if it would be than couldn't execute, because it is deallocated, maybe the assembly Ret or what instruction can't be executed? the return statement.
我会尝试分离委托还是有更好的解决方案?
I will try to separate the delegate or any better solution?
崩溃是典型的:EXC_BAD_ACCESS(code=1,address=090)大会看起来像这样:ldr r1, [r4, r0]
The crash is the typical one: EXC_BAD_ACCESS(code=1,address=090)the Assembly looks like this:ldr r1, [r4, r0]
修改了一点代码,因为在模拟器 4.3 中不会崩溃,但在 5.0 时会崩溃,现在这里是当前代码:
changed a bit the code, because in simulator 4.3 doesn't crash, but at 5.0 it is, now here is the current code:
- (IBAction)showTab:(id)sender {
tab.tabDelegate = self;
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
[self presentModalViewController:tab animated:YES];
}
else{
NSLog(@"Executing presentViewController (ios>= 5.0)");
[self presentViewController:tab animated:true completion: nil];
}
}
-(void)delegateCallback {
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
[self dismissModalViewControllerAnimated:NO];
}
else{
NSLog(@"Executing dismissViewControllerAnimated (ios>= 5.0)");
[self dismissViewControllerAnimated:TRUE completion: nil];//crash
}
}
Edit3 截图:
UIWindowController 转换:fromViewController:toViewController:didEndSeelctor 行崩溃,原因是:没有 parentViewController:https://devforums.apple.com/message/451045伙计们在这里找到了解决方案:https://github.com/ideashower/ShareKit/issues/254 但在保密协议下
UIWindowController transition:fromViewController:toViewController:didEndSeelctor line is crashing, due to: no parentViewController:https://devforums.apple.com/message/451045Guys here found a solution, : https://github.com/ideashower/ShareKit/issues/254 but in under NDA
编辑已解决以重写到适用于 ios 5.0+ 的 PushviewController完整链接:https://stackoverflow.com/a/7767767/529543
Edit solved to revrite to PushviewController for ios 5.0+a heplfull link: https://stackoverflow.com/a/7767767/529543
- (IBAction)presentViewController:(id)sender {
tab.tabDelegate = self;
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
[self presentModalViewController:tab animated:FALSE];
}
else{
NSLog(@"Executing presentViewController (ios>= 5.0) [tab retainCount]: %d " ,[tab retainCount]);
// store parent view to able to restore the state:
parentView = self.view.superview;
// init a navigation controler and set up:
navigationController=[[UINavigationController alloc] initWithRootViewController:self];
[self.view removeFromSuperview];
[myAppDelegate.window addSubview:navigationController.view]; ///appDelegate is delegate of ur Application
navigationController.navigationBar.hidden =true;
[navigationController pushViewController:tab animated:YES];
}
}
然后弹出:
-(void)infoViewDismissed {
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
[self dismissModalViewControllerAnimated:NO];
}
else{
NSLog(@"Executing dismissViewControllerAnimated (ios>= 5.0) ");
[navigationController popToRootViewControllerAnimated:false];
[navigationController.view removeFromSuperview];
[parentView addSubview:self.view];
}
}
我已经解决了我的问题,在一个非常丑陋的模式下,但功能正常......还被告知放弃对 ios3 的支持 :) 我根本不喜欢运行时的 GUI 架构切换.
I have solved my problem, in a very ugly mode, but is functional...Also told to drop the support for ios3 :) I don't like the GUI architecture switch at runtime at all.
推荐答案
你的问题有点难理解,但我猜你有一个retain-cycle:
Your question is a little difficult to understand, but I gather you have a retain-cycle:
ObjectA retains ObjectB
ObjectB retains ObjectA
两个对象都没有被释放?
and neither object gets deallocated?
您的 tabDelegate 属性应为:
Your property for the tabDelegate should read:
@property (nonatomic, assign) id tabDelegate;
// ^^^^^^-This is the important bit, this stops the retain cycle.
这篇关于ios5 上的dismissViewControllerAnimated 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!