如何在没有 XIB 的情况下将选项卡栏添加到现有视图控制器

发布于 2024-08-26 02:24:57 字数 312 浏览 5 评论 0原文

尝试尽可能避免使用 Interface Builder

目前,我通过代码创建了视图控制器,并通过代码更改了视图。

我现在需要其中一个步骤来将应用程序发送到带有标签栏的新视图,这样我也可以更改视图。

理想情况下,我要做的就是告诉当前视图控制器在底部添加一个选项卡栏,但我不确定这是否可行,所以我可能必须将 UIViewController 与 UITabBarController 交换?

任何帮助将不胜感激。

干杯, 安德烈

I'm trying to avoid using Interface Builder as much as possible.

At the moment I have the view controller created via code and change views via code as well.

I now need one of the steps to send the app into a new view with a tab bar, that will allow me to change views as well.

Ideally, what I'd do is tell the current view controller to add a Tab Bar to the bottom, but I'm not sure if that's doable, so I might have to swap the UIViewController with a UITabBarController?

Any help will be appreciated.

Cheers,
Andre

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

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

发布评论

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

评论(1

短暂陪伴 2024-09-02 02:24:57

我手头没有 Xcode,所以我会尝试口头回答。

创建一个新的 UITabBarController 并将当前视图设置为根视图,然后添加所需数量的选项卡(每个选项卡都有自己的视图)。

更新
初始化控制器后,定义一个视图数组(添加顺序很重要)。并在标签栏控制器上调用它

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

UPDATE 2

这是一个简单的代码,用于创建一个带有两个空视图的标签栏,每个视图都有自己的标签按钮。

tabBarController = [[UITabBarController alloc]init];

firstView = [[FirstView alloc] init];
UITabBarItem *item1 = [[[UITabBarItem alloc]initWithTitle:@"First" image:nil tag:1] autorelease];
[firstView setTabBarItem:item1];

secondView = [[SecondView alloc] init];
UITabBarItem *item2 = [[[UITabBarItem alloc]initWithTitle:@"Sec" image:nil tag:1] autorelease];
[secondView setTabBarItem:item2];

[tabBarController setViewControllers:[NSArray arrayWithObjects:firstView,secondView,nil] animated:NO];

[window addSubview:tabBarController.view];

当然,此代码不会按原样有用,您需要手动创建视图,或者为每个视图创建一个 nib 文件并将其加载到 initWithNibName

UPDATE 3
查看斯坦福 iPhone 课程,这是一个斯坦福大学的免费课程。讲师是苹果员工。第 7 讲标题为导航与导航标签栏控制器将为您提供这些组件的良好开端。

I don't have Xcode on hand so I will try to answer verbally.

Create a new UITabBarController and set your current view as the root view, then add as much tabs as you want, (each tab has its own view).

UPDATE
After init'ing the controller, define an array of views (order of adding is important). And call this on the tab bar controller

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

UPDATE 2

Here is a simple code to create a tab bar with two empty views, each has its own tab button.

tabBarController = [[UITabBarController alloc]init];

firstView = [[FirstView alloc] init];
UITabBarItem *item1 = [[[UITabBarItem alloc]initWithTitle:@"First" image:nil tag:1] autorelease];
[firstView setTabBarItem:item1];

secondView = [[SecondView alloc] init];
UITabBarItem *item2 = [[[UITabBarItem alloc]initWithTitle:@"Sec" image:nil tag:1] autorelease];
[secondView setTabBarItem:item2];

[tabBarController setViewControllers:[NSArray arrayWithObjects:firstView,secondView,nil] animated:NO];

[window addSubview:tabBarController.view];

Of course this code won't be useful as is, you will need to create the views manually, or create a nib file for each view and load it in initWithNibName

UPDATE 3
Check this Stanford iPhone Course, it's a free course from Stanford univ. the lecturers are Apple employees. Lecture 7 titled Navigation & Tab Bar Controllers will give you a good start on those components.

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