代码模式:直接从独立的 .xib 文件(而不是从 MainWindow.xib)加载 TabBarController 对象

发布于 2024-09-30 21:27:51 字数 1138 浏览 0 评论 0原文

我在网上四处查看,但无法找到此问题的可接受的解决方案...

我正在寻找一个简单的代码模式:

从单独的容器加载 TabBarController 对象(带有关联的子视图控制器) .xib 文件,而不是从默认的 MainWindow.xib 自动包含和加载。

在 XCode 术语中,从新的 iPad/iPhone 项目作为“选项卡栏应用程序”开始,目标是解决以下问题

  • :项目
  • 将:TabBarController、TabBar、FirstViewController 和 SelectedSecondViewController 从 MainWindow.xib 移动到新的“TabBarController.xib”文件中
  • 移动后,MainWindow.xib 应仅包含:文件所有者、第一响应者、应用程序委托、
  • TabBarController.xib 中的 窗口、文件所有者和第一响应者分别设置为:UIApplication 和 UIResponder。
  • 将主应用程序委托中的“didFinishLaunchingWithOptions”更改为以下内容:

删除:

     [self.window addSubview:tabBarController.view];

添加:

     UITabBarController *uiTab = [[UITabBarController alloc] initWithNibName:@"TabBarController" bundle:nil]; 
     [self.window addSubview:uiTab.view];

通过这些更改,应用程序将构建并运行,但当 TabBarController 加载时,选项卡栏为“空”——似乎没有任何内容在控制器中。

在调试器中查看时,“init”未正确从数据初始化,或者 .xib 文件中的某些内容未正确设置。

对此的正确解决方案是什么?我意识到还有其他方法可以做到这一点,是的,我让它们在其他应用程序中工作。

然而,我正在寻找的是使用默认项目的特定解决方案,可以用作设置iOS代码的通用模式。

提前感谢任何帮助

  • js

I've looked around online, and haven't been able to find an acceptable solution to this problem...

I'm looking for a simple code pattern:

Load a TabBarController object (with associated subview controllers) from a separate .xib file, instead of including and loading automatically from a default MainWindow.xib.

In XCode terms, starting from a new iPad/iPhone project as a "Tab Bar Application", the goal is to solve the following:

  • Create the project
  • Move: TabBarController, TabBar, FirstViewController, and SelectedSecondViewController from MainWindow.xib, into a new "TabBarController.xib" file
  • After moving, MainWindow.xib should only contain: File's Owner, First Responder, App Delegate, Window
  • In TabBarController.xib, File's Owner and First Responder are set to: UIApplication and UIResponder, respectively.
  • Change "didFinishLaunchingWithOptions" in the main application delegate to the following:

REMOVE:

     [self.window addSubview:tabBarController.view];

ADD:

     UITabBarController *uiTab = [[UITabBarController alloc] initWithNibName:@"TabBarController" bundle:nil]; 
     [self.window addSubview:uiTab.view];

With these changes, the application builds and runs, but when the TabBarController loads the tab bar is "empty" -- there don't appear to be any contents in the controller.

In looking in the debugger, either the "init" isn't initializing from the data correctly, or something in the .xib file is not set correctly.

What is the correct solution to this? I realize there are other ways of doing this, and yes, I have them working in other applications.

What I'm looking for however, is a specific solution using the default project, that can be used as a general pattern for setting up iOS code.

Thanks in advance for any help

  • js

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

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

发布评论

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

评论(2

九歌凝 2024-10-07 21:27:52

我想我知道你在寻找什么,因为我想要同样的东西。

  1. 在界面生成器中创建新的空 xib 文件。
  2. 从库添加到 xib TabBarController
  3. 在 xib 上的此选项卡栏控制器上编辑您需要的任何内容。
  4. 当然,保存...

确定要从哪个视图控制器创建带有选项卡栏控制器的 xib。换句话说,谁是会导致此选项卡栏控制器出现的视图控制器。
我们将该视图控制器称为 ParentViewController

在该视图控制器中,为 TabBarController 创建一个 IBOutlet

回到xib,将文件所有者的身份设置为ParentViewController,当然不要忘记将文件所有者中的选项卡栏控制器的插座连接到xib中的选项卡栏控制器。
保存 xib,然后您就可以出发了。

当您想要显示该选项卡栏时,只需决定要采用哪种方式:模态、弹出或其他方式(不在导航控制器内,因为 Apple 不允许选项卡栏控制器位于导航控制器内)。

当您决定时,只需以呈现任何其他视图控制器的方式呈现您的选项卡栏控制器出口即可。例如:

[self presentModalViewController:self.myTabBarController animated:YES];

I think i know what you are looking for because i want the same thing.

  1. Create New Empty xib file at interface builder.
  2. Add to the xib TabBarController from the library.
  3. Edit whatever you need on this tab bar controller on the xib.
  4. Of course, save...

Determine from which view controller do want to create that xib with tab bar controller. In other words, who is the view controller that will cause this tab bar controller to appear.
Let's call that view controller ParentViewController

In that view controller, create an IBOutlet to a TabBarController.

Back to the xib, make the identity of the File's Owner to the ParentViewController and of course dont forget to hook up the outlet of the tab bar controller in the file's owner to the tab bar controller in the xib.
save the xib and you are ready to go.

When you want to present that tab bar, just decide which way you want to do it: Modally,Popup or something else (Not inside a navigation controller because Apple dont allow tab bar controllers to be inside navigation controllers).

When you decide, just present your tab bar controller outlet the way to present any other view controller. for example:

[self presentModalViewController:self.myTabBarController animated:YES];
眼藏柔 2024-10-07 21:27:52

假设您从“选项卡栏应用程序”模板开始,并将 UITabBarController 和关联的视图控制器移动到您所描述的新笔尖...

在新笔尖中,文件的所有者应设置为您的 AppDelegate 类。然后将 File's Owner 的插座“tabBarController”连接到 UITabBarController。

然后在您的 -[application:didFinishLaunchingWithOptions:] 中,不要删除这一行:

[self.window addSubview:tabBarController.view];

相反,在您的应用程序委托作为所有者之前加载新的笔尖:

[[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];

这将设置您的 tabBarController 属性(因为您在笔尖中建立了该连接)然后你就可以照常进行了。您所做的实际上是创建一个全新的 UITabBarController,并且根本不从笔尖加载该控制器。 (好吧,好吧,您加载了一小会儿,但随后没有用它做任何有用的事情)

希望有所帮助。

Assuming you start with the "Tab Bar Application" template and move the UITabBarController and associated view controllers to a new nib as you described...

In your new nib, File's Owner should be set to your AppDelegate class. Then connect the outlet "tabBarController" of File's Owner to the UITabBarController.

Then in your -[application:didFinishLaunchingWithOptions:], do not remove this line:

[self.window addSubview:tabBarController.view];

Instead, load the new nib right before that with your app delegate as the owner:

[[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];

That will set your tabBarController property (since you made that connection in the nib) and then you can proceed as normal. What you were doing was actually creating a whole new UITabBarController, and not loading the one from the nib at all. (well, ok you were loading it for a brief moment, but then not doing anything useful with it)

Hope that helps.

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