让 UINavigationController 自动适应它的呈现方式

发布于 2024-12-10 19:29:24 字数 1082 浏览 0 评论 0原文

我正在开发一个可以以模态方式呈现或推入导航堆栈的视图控制器。我将其设为 UINavigationController 子类,以便我免费获得所有 UIToolbar 内容。我可以使用模态方式呈现它:

[self presentModalViewController:myViewController animated:YES];

问题是, UINavigationController 不允许将另一个 UINavigationController 推入其中(有道理),因此这会使其崩溃:

[self.navigationController pushViewController:myViewController animated:YES];

是否有办法检测 myViewController 的呈现方式并自动让它在 UINavigationControllerUIViewController 之间切换,这样我就不需要 2 个不同的类?

换句话说,myViewController 将能够检测它的呈现方式,并将其推送到以下内容:

[self.navigationController pushViewController:myViewController.topViewController animated:YES];


注意:类似这样的事情可能会做,但它离默认的 UIViewController 行为太远了:

[myViewController pushIntoNavigationController:navController]; // only push myViewController.topViewController
[myViewController presentModallyInParentController:parentController]; // push the whole myViewController

I'm working on a view controller that can be presented modally or pushed into a navigation stack. I made it a UINavigationController subclass so that I get all the UIToolbar stuff for free. I can present it modally using:

[self presentModalViewController:myViewController animated:YES];

Problem is, UINavigationController doesn't allow pushing another UINavigationController into it (makes sense), so this crashes it:

[self.navigationController pushViewController:myViewController animated:YES];

Would there be a way to detect how myViewController is presented and automatically have it switch between UINavigationController and UIViewController accordingly so that I don't need 2 different classes?

In other words, myViewController would be able to detect how it's getting presented and pushing it would come down to something like:

[self.navigationController pushViewController:myViewController.topViewController animated:YES];

NOTE: Something like this would probably do, but it's getting too far away from the default UIViewController behaviors:

[myViewController pushIntoNavigationController:navController]; // only push myViewController.topViewController
[myViewController presentModallyInParentController:parentController]; // push the whole myViewController

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

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

发布评论

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

评论(1

少女的英雄梦 2024-12-17 19:29:24

myViewController 子类中,创建一个如下所示的方法:

- (void)presentFromViewController:(UIViewController *)presentingViewController
{
    if ([[presentingViewController class] isEqual:[UINavigationController class]])
        [presentingViewController pushViewController:self.topViewController animated:YES];
    else
        [presentingViewController presentModalViewController:self animated:YES];
}

这可行吗还是我理解不正确?

In the myViewController subclass, create a method something like this:

- (void)presentFromViewController:(UIViewController *)presentingViewController
{
    if ([[presentingViewController class] isEqual:[UINavigationController class]])
        [presentingViewController pushViewController:self.topViewController animated:YES];
    else
        [presentingViewController presentModalViewController:self animated:YES];
}

Would this work or am I not understanding correctly?

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