dismissModalViewControllerAnimated :(和dismissViewControllerAnimated)在iOS 5中崩溃

时间:2011-10-19 12:53:32

标签: iphone uiviewcontroller ios5 dismiss

我找不到任何合理的解释,但事实仍然是,在iOS 5(xCode 4.2)中,如果我presentModalView:* animated:YES,我可以调用dismissModalViewAnimated:*很好,但是如果我调用presentModalView:*动画:不,然后调用dismiss方法崩溃。 (如果我使用新的presentViewController,它的工作原理相同:animated:completion:+ dismissViewControllerAnimated :)。我现在正在尝试解决这个问题(我不希望演示文稿动画)并向Apple报告一个错误,但我已经打了一段时间。欢迎任何和所有建议。在iOS 5上没有多少,所以请尽可能帮助。在iOS 4或iOS 5中不会崩溃的示例代码:

LoginController *loginController = [[LoginController alloc] initWithNibName:@"LoginControllerGG" bundle:nil];
[self presentModalViewController:loginController animated:YES];
[loginController release];
...
[self dismissModalViewControllerAnimated:YES];

在解雇调用中,这将在iOS 5中使用EXC_BAD_ACCESS崩溃:

LoginController *loginController = [[LoginController alloc]    initWithNibName:@"LoginControllerGG" bundle:nil];
[self presentModalViewController:loginController animated:NO];
[loginController release];
...
[self dismissModalViewControllerAnimated:YES]; //crashes with EXC_BAD _ACCESS

一个注意事项:我在loginController中有一个动画,它发生在viewDidLoad上。去看看是否会改变任何东西,但我想把它拿出去,因为我需要一个解决方案尽快。


[编辑]完整的代码流程...在AppDelegate中,应用程序:didFinishLaunchingWithOptions:

if (!loggedIn)  [myViewController showLoginPanel];

在myViewController中:

- (void)showLoginPanel {    
    LoginController *loginController = [[LoginController alloc] initWithNibName:@"LoginControllerGG" bundle:nil];
    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
        [self presentViewController:loginController animated:NO completion:nil];
    } else {
        [self presentModalViewController:loginController animated:NO]; //iOS 4 works fine with or without animation   
    } 
    [loginController release];  
}

在loginController中:

- (IBAction)closeLoginWindow {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CloseLoginWindow" object:nil];
}   //doing it this way because calling on the self.parentViewController doesn't work

回到myViewController:

- (void) viewDidLoad
    ...
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeLoginWindow) name:@"CloseLoginWindow" object:nil];
    ...

- (void)closeLoginWindow {
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
        [self dismissViewControllerAnimated:YES completion:nil];    //iOS 5 crashes only if presentation was not animated
    } else [self dismissModalViewControllerAnimated:YES];    //deleting the previous condition, iOS 5 still crashes if presentation was not animated
}    

2 个答案:

答案 0 :(得分:4)

在iOS5中,生命周期的管理有所改变,我无法详细解释这个问题。无论如何,修复是将applicationDidFinishLaunchingWithOptions的工作流推迟到applicationDidBecomeActive。似乎在applicationDidFinishLaunchingWithOptions调用时没有初始化某些内容。

- (void)applicationDidFinishLaunchingWithOptions:... {    
    // in order to do this only at launching, but not on every activation 
    // Declaration as property for example
    applicationDidLaunch = YES;
}

- (void) applicationDidBecomeActive:(UIApplication *)application {
    if (applicationDidLaunch) {
        applicationDidLaunch = NO;
        [Start your login Workflow with modal view presenting here]
    }
}

好奇你的反馈:)....

答案 1 :(得分:2)

我将加上我的2美分:我有ImagePickerController,只有当我没有手动释放选择器(IOS 5 SDK)时,才能解除它的工作。

因此。对于你的情况,我可以提供这样的解决方法: 1.删​​除行 - [loginController release]; 2.防止内存泄漏将loginController作为属性添加到当前控制器并仅在当前控制器的dealloc()中释放它:

@interface myViewController : UIViewController 

@property (nonatomic, retain) LoginController *loginController;

@end

...

@implementation myViewController

- (void)showLoginPanel {    
    self.loginController = [[LoginController alloc] initWithNibName:@"LoginControllerGG" bundle:nil];
     // ... something goes here  
}

-(IBAction)loginClose() 
{
    // this should close all windows as far as you call it from current (main) controller
    [self dismissModalViewControllerAnimated:YES]; 
    // ... then anything you want EXCEPT [loginController release];
}

-(void)dealloc() 
{
    [loginController release];
}

@end
祝你好运:)

P.S。我刚刚写了这个,所以这只是一个想法如何欺骗它。 Somebosy可能会纠正我......虽然无论如何它对我有用。