简体   繁体   English

iPhone开发人员视图交换

[英]iPhone dev view swapping

So I'm trying to find out the best way to swap views for an iPhone game I'm making. 因此,我正在尝试寻找交换正在制作的iPhone游戏的最佳方法。 I have a "root view controller" that has a reference to all the view controllers I want to swap between. 我有一个“根视图控制器”,它引用了我要在其中交换的所有视图控制器。 So I add the main menu view to this root view controller - [self.view addSubview:mainMenuController.view]; 因此,我将主菜单视图添加到此根视图控制器-[self.view addSubview:mainMenuController.view]; - Then in the main menu view I have an instructions button. -然后在主菜单视图中,有一个指令按钮。 I define an IBAction in the mainMenuController to respond to the instructions button being clicked. 我在mainMenuController中定义一个IBAction来响应被单击的指令按钮。 From there, I call [self.view removeFromSuperview] which works great. 从那里,我调用[self.view removeFromSuperview],效果很好。 It gets rid of the main menu. 它摆脱了主菜单。 So next I want to add the instructions view. 接下来,我要添加说明视图。 I figured that it would be as simple as [super.view addSubview:super.instructionsController.view], but no luck! 我认为这将像[super.view addSubview:super.instructionsController.view]一样简单,但是没有运气! I've thought of a few ways to get around this, but they all seem very inelegant, as I would like to keep all references to my view controllers in one place, the root view controller. 我已经考虑了一些解决方法,但是它们似乎都很笨拙,因为我想将对视图控制器的所有引用都放在一个位置,即根视图控制器。 Any thoughts? 有什么想法吗?

Could you not have your main menu view controller tell the root view controller that it's closed, along with a reference to what view it should load next? 您能否不让主菜单视图控制器告知根视图控制器已关闭,并提供对下一个应加载的视图的引用? I'm assuming you're not doing something akin to what UITableView does, in which case you'd likely want to use a stack. 我假设您没有做类似于UITableView的操作,在这种情况下,您可能希望使用堆栈。

Are you mixing super (an Objective-C keyword) with self.superview (an UIView property)? 您是否正在将super(Objective-C关键字)与self.superview(UIView属性)混合在一起?

At any rate it's indeed a better design to inform your main controller to switch to another view, rather than making that decision from the added subview. 无论如何,通知主控制器切换到另一个视图确实是一个更好的设计,而不是从添加的子视图中做出决定。

我只是通过在每个控制器中将一个委托属性设置为根视图控制器来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM