1

代码会因为循环引用而崩溃吗?

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。

尽管 [self.tabDelegate myViewDismissed] 之后不再是代码;但是如果它不能执行,因为它被释放了,也许程序集 Ret 或者什么指令不能执行?返回声明。

我会尝试将代表分开还是有更好的解决方案?

编辑:崩溃是典型的:EXC_BAD_ACCESS(code=1,address=090) 程序集如下所示:ldr r1, [r4, r0]

Edit2:更改了一点代码,因为在模拟器 4.3 中不会崩溃,但在 5.0 时会崩溃,现在这里是当前代码:

- (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/问题/254,但在保密协议下

编辑已解决以重新写入 ios 5.0+ 的 PushviewController 的完整链接: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 架构切换。

4

2 回答 2

3

您的问题有点难以理解,但我认为您有一个保留周期:

ObjectA retains ObjectB
ObjectB retains ObjectA

并且两个对象都没有被释放?

您的 tabDelegate 属性应为:

@property (nonatomic, assign) id tabDelegate;
//                    ^^^^^^-This is the important bit, this stops the retain cycle.
于 2012-08-06T17:32:20.320 回答
1

没有更多信息很难说(您是否使用 ARC,您是否保留/分配了委托等),但根据 iOS 文档,您也在使用已弃用的 modalview 方法。可能值得尝试:

[self presentViewController:tab animated:YES completion:NULL];

[self dismissViewControllerAnimated:YES completion:NULL];
于 2012-08-06T17:47:02.460 回答