加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 如何以编程方式创建tabbar并在其上添加按钮

发布时间:2020-12-15 01:42:51 所属栏目:百科 来源:网络整理
导读:我想在我的视图中以编程方式在我的标签栏上添加按钮… 我有导航控制器,但它不允许我添加这些.. 我想在我的视图中以编程方式创建… 解决方法 由于标签栏控制器是一个容器视图控制器,用于将应用程序划分为两种或多种不同的操作模式,因此大多数应用程序都将导航
我想在我的视图中以编程方式在我的标签栏上添加按钮…

我有导航控制器,但它不允许我添加这些..
我想在我的视图中以编程方式创建…

解决方法

由于标签栏控制器是一个容器视图控制器,用于将应用程序划分为两种或多种不同的操作模式,因此大多数应用程序都将导航控制器作为标签栏控制器的子项.

Apple的立场是这样的:

You use tab bar controllers in
situations where your application
either presents different types of
data or presents the same data in
significantly different ways.

这并不是说你不能以不同的方式做事……你的主要问题是你已经在应用程序中放置了一个Nav Controller,并且你想以编程方式创建标签栏控制器.因此,我可以看到这一点的唯一方法是,您不介意每次更改导航控制器中的屏幕时标签栏控制器是否更改.有些应用以这种方式工作.大多数人没有.

如果我的上述假设是正确的,我建议您重新考虑您的代码,看看您是否想要进行这种开发.如果是这样,您可以轻松创建标签栏控制器并将其附加到当前视图中.

以下是我用于为我的某个应用创建设置的代码:

// set up a local nav controller which we will reuse for each view controller
UINavigationController *localNavigationController;

// create tab bar controller and array to hold the view controllers
UITabBarController *tabBarController = [[UITabBarController alloc] init];

NSMutableArray *localControllersArray = [[NSMutableArray alloc] initWithCapacity:1];

// setup the first view controller (Root view controller)
RootViewController *myViewController;
myViewController = [[RootViewController alloc] initWithTabBar];

// create the nav controller and add the root view controller as its first view
localNavigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
localNavigationController.navigationBar.barStyle = UIBarStyleBlack;
localNavigationController.delegate = self;

[localControllersArray addObject:localNavigationController];

// release since we are done with this for now
[localNavigationController release];
[myViewController release];

tabBarController.viewControllers = localControllersArray;
tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleBlack;  

tabBarController.delegate = self;
tabBarController.moreNavigationController.delegate = self;

// release the array because the tab bar controller now has it
[localControllersArray release];

self.tabBarController.selectedIndex = 0;

// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];

// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];

在很多情况下,我觉得以编程方式完成所有操作更容易.

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读