presentModalViewController无法正常工作 [英] presentModalViewController not working properly

查看:57
本文介绍了presentModalViewController无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个视图。

我要执行以下操作:

A presents B modally
A dismisses B
A presents C modally

我设置了一个委托模式,其中A是B的委托。这是我在B中提出和解雇的方式:

I have setup a delegate pattern where A is B's delegate. This is how I am presenting and dismissing in B:

[delegate dismissB]; //this is just [self dismissModalViewControllerAnimated:NO]
[delegate presentC]; //this is just [self presentModalViewController:c animated:NO];

由于某种原因,当我在没有调试器结果的情况下执行此代码时,我的应用程序崩溃了(我有NSZombieEnabled)。

For some reason my app crashes when I execute this code with no debugger results (I have NSZombieEnabled).

当我注释掉 [代表presentC] 时,应用程序将正确关闭B。 当我注释掉 [delegate dismissB] 时,即使该行执行了,该应用也没有执行任何操作。我不确定为什么?

When I comment out [delegate presentC] the app will dismiss B properly. When I comment out [delegate dismissB] the app does nothing, even though the line executes. I am not sure why?

更新:
这是A中的代码

UPDATE: Here is the code in A

-(void)showARView{
    [self dismissModalViewControllerAnimated:NO];

    ARViewController* arViewController = [[[ARViewController alloc] initWithNibName:@"ARViewController" bundle:nil]autorelease];
    UINavigationController *arNavController = [[UINavigationController alloc] initWithRootViewController:arViewController];

    LeaderBoardTableViewController* lbViewController = [[[LeaderBoardTableViewController alloc] initWithNibName:@"LeaderBoardTableViewController" bundle:nil]autorelease];
    lbViewController.title = @"Leaderboard";    
    UINavigationController *lbNavController = [[UINavigationController alloc] initWithRootViewController:lbViewController];

    arTabBarController = [[UITabBarController alloc] init];//initWithNibName:nil bundle:nil];
    arTabBarController.delegate = self;
    arTabBarController.viewControllers = [NSArray arrayWithObjects:arNavController, lbNavController, nil];
    arTabBarController.selectedViewController = arNavController;

    [arNavController release];
    [lbNavController release];

    [self presentModalViewController:arTabBarController animated:NO];
}

这是B中的代码

[delegate showARView];


推荐答案

当您调用dismissB时,委托将解散viewcontroller。在正常情况下(如果您不将其保留在其他地方),这将导致视图控制器被同步释放。然后,您尝试访问委托实例变量,但是为此,代码需要一个合理的(隐藏的)自指针,该指针已被释放。我不确定在这种情况下NSZombie是否可以提供帮助。通过在 [delegate dismissB];之前插入 [[自我保留] autorelease]; ,您可以轻松找出这是否是导致崩溃的原因。 / code>。但是,这是一个破解,而不是修复。您有一个设计问题。

When you call dismissB the delegate dismisses the viewcontroller. Under normal circumstances (if you do not retain it elsewhere) this leads to the viewcontroller to be dealloced synchronously. And afterwards you are trying to access the delegate instancevariable, but for this the code needs a sane (hidden) self pointer, which is dealloced. I'm not sure if NSZombie can help in this case. You can easily find out if this is the reason for your crash, by inserting [[self retain] autorelease]; before [delegate dismissB];. However this is a hack and not a fix. You have a design problem.

这不是使用委托的方式。 B提供一些用户界面并接收一些用户交互。然后它应该通过委托消息(例如G)告诉A发生了什么。 bWasCanceled或bFinished。在您的情况A中,代表有责任决定下一步要做什么。因此,在您的情况下,代表可以决定解雇B而是出示C。或者在代码中:

This is not the way delegates are meant to be used. B presents some userinterface and receives some user interaction. It should then tell A what has happened via a delegate message, e.G. bWasCanceled or bFinished. It is the duty of the delegate, in your case A, to decide what to do next. So in your case the delegate may decide to dismiss B and instead present C. Or in code:

// Inside A
- (void)controllerB:(UIViewController*)ctl didFinishWithResult:(id)something {
    [self dismissModalViewControllerAnimated:NO];
    // Instantiate and initialize c
    [self presentModalViewController:c animated:NO];
}

// Inside B
[delegate controllerB:self didFinishWithResult:@"OK"];

如果我完全误解了您的代码,并且一切正常,那么我还有其他建议。在运行循环的同一回合中解雇和展示模态视图控制器时,我已经看到了奇怪的问题。您可以尝试 [使用对象:nil afterDelay:0.0来委托performSelector:@selector(presentC);; ,看看是否有帮助。

If I completely misinterpreted your code, and everything is fine there, I have an other suggestion. I have seen strange issues when dismissing and presenting modal viewcontrollers in the same round of the runloop. You may try [delegate performSelector:@selector(presentC) withObject:nil afterDelay:0.0]; and see if that helps.

这篇关于presentModalViewController无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆