iPad:呈现模式视图,而我的parentViewController 现在为零?

发布于 2024-12-18 13:53:57 字数 1309 浏览 2 评论 0原文

我有几个模态视图一直工作得“很好”,现在停止返回到父视图控制器,并且“代码没有改变”。 ——经典问题描述。

我调试了模态视图关闭,父视图控制器为零,这解释了问题,但没有解释原因。我确实将 SDK 从 4.1.2 升级到了 4.2,这样我就可以开始使用 iOS 5。我对新的内存管理 ARC 以及我的自动释放与保留/释放风格持怀疑态度。

以下是从我的 rootview 控制器到 AboutViewController 的代码:

- (IBAction)doInfo:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    AboutViewController *aboutViewController = [[[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:[NSBundle mainBundle]] autorelease];
    if (aboutViewController) {
        aboutViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        aboutViewController.hidesBottomBarWhenPushed = YES;
        self.navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        self.navigationController.navigationBarHidden = YES;
        [self presentModalViewController:aboutViewController animated:YES];
    }
}

以下是按“完成”按钮后,AboutViewController 中的关闭返回到其父级。

- (IBAction)doDone:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
}

好的...我通过如下更改进行了修复。现在的问题是为什么这之前有效?

- (IBAction)doDone:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    [self dismissModalViewControllerAnimated:YES];
}

I have a couple of modal views that have been working "just fine" and now stopped returning to the parent view controller and "the code has not changed." -- classic problem description.

I debugged the modal view dismissing and the parent view controller is nil, which explains the problem, but not the cause. I did upgrade my SDK from 4.1.2 to 4.2 so I can start working with iOS 5. I am suspect of the new memory management ARC and my style of autorelease versus retain/release.

Following is the code from my rootview controller to the AboutViewController:

- (IBAction)doInfo:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    AboutViewController *aboutViewController = [[[AboutViewController alloc] initWithNibName:@"AboutViewController" bundle:[NSBundle mainBundle]] autorelease];
    if (aboutViewController) {
        aboutViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        aboutViewController.hidesBottomBarWhenPushed = YES;
        self.navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        self.navigationController.navigationBarHidden = YES;
        [self presentModalViewController:aboutViewController animated:YES];
    }
}

Following is the dismiss in the AboutViewController back to its parent after pressing a "Done" button.

- (IBAction)doDone:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    [[self parentViewController] dismissModalViewControllerAnimated:YES];
}

OK ... I fixed by changing as follows. Now the question is why did this work before?

- (IBAction)doDone:(id)sender {
    NSLog(@"%s", __FUNCTION__);
    [self dismissModalViewControllerAnimated:YES];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心作怪 2024-12-25 13:53:57

引用 [1] 关于属性 parentViewController

在 iOS 5.0 之前,如果视图没有父视图控制器并且以模态方式呈现,则呈现它的视图控制器将是回来了。现在情况已不再如此。您可以使用presentingViewController属性获取呈现视图控制器。

因此这解决了为什么它以前有效但现在不起作用的问题。如果针对 iOS 5 进行构建,我将使用 presentingViewController 属性,因为文档建议让父视图控制器(或在本例中为演示)关闭模态视图。但是,如果针对 iOS 4 和 5 进行构建,我会让您的代码就位,因为它会将调用转发到适当的视图控制器。

无论如何,如果应用程序在重新编译之前在 iOS 5 上完美运行,我会认为 Apple 采取了一些运行时技巧来模仿 iOS5 之前编译的应用程序的旧行为。

[1] http://developer.apple.com/library/ ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

Quoting from [1] about the property parentViewController:

Prior to iOS 5.0, if a view did not have a parent view controller and was being presented modally, the view controller that was presenting it would be returned. This is no longer the case. You can get the presenting view controller using the presentingViewController property.

So this resolves the issue why this did work before and does not anymore. If building for iOS 5, I would use the presentingViewController property, as it is advised by the docs to let the parent view controller (or in this case presenting) dismiss the modal view. However if building for iOS 4 and 5, I would let your code in place as it forwards the call to the appropriate view controller anyway.

If in any case the app ran flawlessly on iOS 5 before recompiling, I would assume that Apple put some runtime-trickery in place that mimics the old behaviour for apps compiled pre-iOS5.

[1] http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

半寸时光 2024-12-25 13:53:57

我构建了一个在 iOS 4 上添加 presentingViewController 的类别。

它在 iOS 5 上自行禁用。

您可以无缝使用它。请参阅后向模态

我希望这对你和我一样有好处;它使您的代码更加干净!

I have built a category that add presentingViewController on iOS 4.

It disables itself on iOS 5.

You can use it seamlessly. Please see backward-modal.

I hope this benefits you as much as it does to me; It makes your code more clean!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文