在 UIView 中加载不同的视图控制器(如 iFrame for Xcode)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11552413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:56:23  来源:igfitidea点击:

Load different view controllers inside UIView (Like iFrame for Xcode)

iphoneiosxcodeuiviewuiviewcontroller

提问by felix_xiao

I am in the process of making my first iPhone app with storyboard in Xcode and am stuck. My main screen has a view controller called MemberViewController, which is the home screen. This screen has a UIView that is smaller than the view controller called mainContent.

我正在用 Xcode 制作我的第一个带有故事板的 iPhone 应用程序,但被卡住了。我的主屏幕有一个名为 MemberViewController 的视图控制器,它是主屏幕。这个屏幕有一个 UIView,它比名为 mainContent 的视图控制器小。

So basically, I want to be able to load different view controllers (all the same size as the UIView) inside the UIView.

所以基本上,我希望能够在 UIView 中加载不同的视图控制器(所有与 UIView 相同的大小)。

MemberViewController (home page) -mainContent (UIView)

MemberViewController(主页)-mainContent(UIView)

GetStartedViewController (separate view controller that I want to show inside the UIView) ProfileViewController (separate view controller that I want to show inside the UIView)

GetStartedViewController(我想在 UIView 中显示的单独视图控制器) ProfileViewController(我想在 UIView 中显示的单独视图控制器)

For example, I want the GetStartedViewController to have a button that I can press to switch to the ProfileViewController. The view controllers need to be able to replace each other inside the mainContent UIView.

例如,我希望 GetStartedViewController 有一个按钮,我可以按下该按钮切换到 ProfileViewController。视图控制器需要能够在 mainContent UIView 中相互替换。

Thank you so much in advance for your help. If there's an easier way to do this, please let me know.

非常感谢您的帮助。如果有更简单的方法可以做到这一点,请告诉我。

采纳答案by Nick Esposito

I use this technique a lot actually. My typical setup of a root UIViewController(you call it memberController) has a view with a UITabBarat the bottom, and then another UIView(you call it mainContent) which contains the rest of the space above that bar.

我实际上经常使用这种技术。我对根的典型设置UIViewController(您称之为memberControllerUITabBar在底部有一个视图,然后是另一个UIView(您称之为mainContent),其中包含该栏上方的其余空间。

memberControllerstays on the screen all the time. Inside of mainContent, add a UINavigationControllerand initialize it with your first content-carrying GetStartedViewController. When you want to switch tabs on your tab bar, send the appropriate message to this UINavigationControllerand the views will change inside.

memberController一直停留在屏幕上。在 中mainContent,添加 aUINavigationController并使用您的第一个内容进行初始化GetStartedViewController。当你想在你的标签栏上切换标签时,向它发送适当的消息,UINavigationController视图就会在里面发生变化。

HINT: say your UINavigationControlleris called navController- you can get rid of the navigation bar (blue one at the top) by sending the message [navController setNavigationBarHidden:TRUE];

提示:说你UINavigationController的电话navController- 你可以通过发送消息来摆脱导航栏(顶部的蓝色)[navController setNavigationBarHidden:TRUE];

EDIT: The code you requested looks like this. This adds a nav controller to a window's view in the applicationDidFinishLaunchingWithOptionsmethod. Instead of window, just do this same thing on your view controller in the viewDidLoad.

编辑:您请求的代码如下所示。这会向方法中的窗口视图添加一个导航控制器applicationDidFinishLaunchingWithOptions。而不是窗口,只需在viewDidLoad.

windowand navControllerare both properties

window并且navController都是属性

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.navController = [[UINavigationController alloc] initWithRootViewController:[YourViewController new]];
[self.navController setNavigationBarHidden:YES];
[self.window addSubview:self.navController.view];
[self.window makeKeyAndVisible];

Hope this helps!

希望这可以帮助!