在ios5上dismissViewControllerAnimated崩溃

时间:2012-08-06 17:22:35

标签: objective-c ios ios5 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。

虽然在[self.tabDelegate myViewDismissed]之后不再有任何代码;但如果它不能执行,因为它被解除分配,可能是程序集Ret或什么指令不能被执行?退货声明。

我会尝试将代表或任何更好的解决方案分开?

编辑: 崩溃是典型的:EXC_BAD_ACCESS(代码= 1,地址= 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截图:

threads

UIWindowController转换:fromViewController:toViewController:didEndSeelctor行崩溃,原因是:没有parentViewController: https://devforums.apple.com/message/451045 伙计们在这里找到了一个解决方案:https://github.com/ideashower/ShareKit/issues/254但在NDA下

编辑解决以重新启动到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架构在运行时切换。

2 个答案:

答案 0 :(得分:3)

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

ObjectA retains ObjectB
ObjectB retains ObjectA

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

tabDelegate的属性应为:

@property (nonatomic, assign) id tabDelegate;
//                    ^^^^^^-This is the important bit, this stops the retain cycle.

答案 1 :(得分:1)

很难说没有更多信息(你使用ARC,你是保留/分配代表等等)但是根据iOS文档,你也使用了弃用的modalview方法。值得一试:

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

[self dismissViewControllerAnimated:YES completion:NULL];