9

我找不到任何合乎逻辑的解释,但事实仍然是,在 iOS 5(xCode 4.2)中,如果我 presentModalView:* animated:YES,我可以调用dismissModalViewAnimated:* 很好,但是如果我调用 presentModalView:* animated:NO ,然后调用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];  
}

在登录控制器中:

- (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
}    
4

2 回答 2

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]
    }
}

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

于 2011-10-19T15:32:28.840 回答
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

祝你好运 :)

PS我刚刚写了这个,所以这只是一个如何作弊的想法。Somebosy 可能会纠正我……尽管无论如何它对我有用。

于 2012-01-11T23:44:03.603 回答