gpt4 book ai didi

ios - 如何以编程方式在 View 之间转换?

转载 作者:行者123 更新时间:2023-11-29 04:27:27 25 4
gpt4 key购买 nike

我正在开发一款游戏,我想在多个 View 之间进行转换,例如菜单屏幕、游戏屏幕、游戏结束屏幕等。最简单的方法是什么?我不确定是否应该使用 View 堆栈,因为 View 的显示顺序并不总是相反。

最佳答案

我假设“ View 堆栈”是指 UINavigationController?

最简单的方法是将所有 View Controller 的引用保留在某个地方,例如,我看到人们经常使用应用程序委托(delegate),因此您的应用程序委托(delegate)的类扩展看起来有点像:

@interface AppDelegate ()

@property (nonatomic, strong) UIViewController *rootViewController; //this is what gets set as the window's root VC
@property (nonatomic, strong) UIViewController *mainScreenViewController;
@property (nonatomic, strong) UIViewController *gameScreenViewController;
@property (nonatomic, strong) UIViewController *gameOverScreenViewController;

@end

假设 rootViewController 只是控制应用程序其余部分的容器 View (尽管将所有这些逻辑放入 Root View Controller 中,您实际上可能会得到很好的服务......)

现在,只要您需要显示某个屏幕,请调用如下方法:

- (void)switchToViewController:(UIViewController *)viewController
{
[self.rootViewController.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.rootViewController.view addSubview:viewController.view];
}

您现在可以编写命名更容易记住的方法,例如 -switchToGameOverScreen

- (void)switchToGameOverScreen
{
[self switchToViewController:self.gameOverScreenViewController];
}

这种 View 导航的基本模式大致可以在 UITabBarController 中找到,并且通常在由 UISegmentedControl 控制的 View 中找到。

希望这有帮助!

关于ios - 如何以编程方式在 View 之间转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12135090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com