如何将选项卡栏控制器放在视图控制器而不是委托内部?
我有一个非常基本的 iPhone 应用程序,其中发生以下步骤。
- 加载...游戏根视图控制器的应用程序委托
- (这只是 2 个按钮,“开始游戏”和“继续游戏”)
- 当单击开始游戏按钮时,加载 RootViewController。
- 带有标签栏控制器的根视图控制器?
我真的不确定游戏如何在 iPhone 应用程序中做到这一点,但以上是我最好的猜测。
我希望选项卡栏控制器仅出现在 RootViewController 内,但无论我做什么,我都无法使选项卡栏控制器出现在该视图内。
不过,我可以让正常的选项卡栏出现。但这不是选项卡栏控制器,我需要控制每个选项卡的功能(导航控制器、表格控制器等)。
根据 文档 的多种方法之一通过应用程序的主窗口使用选项卡栏控制器。但它并没有说明是否可以在普通视图中使用它;或者确实如何。
我尝试了几次尝试使选项卡栏控制器看起来无济于事。
例如,在我的 GameRootViewController 内的按钮中,如果我这样做;
-(IBAction) btnPress {
RootViewController *root = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
[self.view addSubview:root.tabBarController.view];
[root release];
}
什么也没有出现。
如果我尝试正常的做法:
[self.view addSubview:root.view];
视图出现,但我从未看到选项卡栏控制器。
这让我很困惑。
我在想游戏/应用程序可能可以这样做:
- 应用程序委托
- 加载所有启动画面、制作人员、制作者...等
- 加载开始游戏、继续游戏屏幕并处理这些创建的操作
- 使应用程序委托窗口添加标签栏控制器到 self.window。
但我不确定。我想澄清/了解是否可以将选项卡栏控制器放在视图控制器内部,或者它们只能在应用程序委托内部使用?
I have a very basic iphone app where the following steps occur.
- App Delegate which loads...
- Game root view controller (this is just 2 buttons, "start game" and "continue game")
- When the start game button is clicked load RootViewController.
- Root View Controller with a tab bar controller??
I'm really not sure how games do this in iPhone apps but the above is my best guess.
I want the tab bar controller only to appear inside the RootViewController, but regardless of what I do I cannot make the tab bar controller appear inside this view.
I can however make normal tab bars appear. But this is not a tab bar controller, I need control over what each of these tabs do (navigation controller, table controller, etc).
According to the docs one of the many ways of using a tab bar controller is via the application’s main window. But it doesn't say whether you can use it in a normal view; or indeed how.
I tried several attempts to make the tab bar controller appear to no avail.
For example, in my GameRootViewController inside the button if I do this;
-(IBAction) btnPress {
RootViewController *root = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
[self.view addSubview:root.tabBarController.view];
[root release];
}
Nothing appears.
If I try the normal way of doing:
[self.view addSubview:root.view];
The view appears but I never see the tab bar controller.
This makes me very confused.
I'm thinking possibly that games/apps could do this:
- App Delegate
- Load all the splash screens, credits, made by...etc
- Load the start game, continue game screen and handle the actions these create
- Make the app delegate window add the tab bar controller to the self.window.
But I am not sure. I want to clarify/understand whether I can put tab bar controllers inside of view controllers, or can they only ever be used inside the app delegate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,不要将控制器与视图混淆。视图是 UI 的可见部分,控制器是幕后不可见的对象。您不要将控制器放在视图内部或将控制器放在其他控制器“内部”。相反,控制器存在于各种层次结构中,这些层次结构的结构决定了哪些视图出现在何时何地。
您以非标准方式使用选项卡栏(这可能会让用户感到困惑),但您需要将选项卡栏控制器放入导航控制器中,以便获得如下所示的层次结构:
因此,您的第一个可见视图是按钮视图,它是由navigationController 的rootController 控制。选择按钮后,它将 TabViewController 推送到导航堆栈上,从而将选项卡栏带入视图并选择默认选项卡。 (请注意,您必须隐藏导航栏才能使导航堆栈对用户不可见。)
Firstly, don't confuse controllers with views. Views are visible parts of the UI, controllers are invisible objects behind the scenes. You don't put controllers inside of views or controllers "inside" other controllers. Instead, controllers exist in various hierarchies and the structure of those hierarchies dictates which views appear when and where.
You are using a tabbar in a none standard way (which may confuse users) but you need put the tabbar controller inside of a navigation controller so that you get an hierarchy that looks like:
So, you first visible view is the button view which is controlled by the rootController of the navigationController. Upon selecting a button, it pushes the TabViewController onto the nav stack which brings the tabbar into view with it's default tab selected. (Note you will have to hide the navigation bar to make the navigation stack invisible to the user.)