如何使用 Objective C 在 iPhone 中添加 UITabBar

发布于 2024-10-18 08:21:45 字数 51 浏览 6 评论 0原文

如何以编程方式为 iPhone 应用程序添加 UITabBar。需要一些建议和示例代码。

How to add add UITabBar programmatically for iphone app. Need some suggestion and sample code.

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

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

发布评论

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

评论(4

九命猫 2024-10-25 08:21:45

这里有两个方面......

第一个方面(UITabBarController):
在这里,您可以将多个类(即 Viewcontrollers)添加到 UITabBarController,如前面的答案所示...

第二个方面(UITabbar):
在这里,您可以在任何视图控制器中添加选项卡栏...对于此选项卡栏,您可以添加多个 UITabBar 项目...并且您可以获得单独的选项卡栏项目操作...

请参阅下面的 UITabBar 代码

---------> 添加UITabBar

UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
myTabBar.delegate=self;   //here you need import the protocol <UITabBarDelegate>
[self.view addSubview:myTabBar];

---------> 向 UITabBar 添加项目

NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];

UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Item1" image:[UIImage imageNamed:@"Item1image.png"] tag:0];
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1 ];
UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2 ];

[tabBarItems addObject:tabBarItem1];
[tabBarItems addObject:tabBarItem2];
[tabBarItems addObject:tabBarItem3];

myTabBar.items = tabBarItems;
myTabBar.selectedItem = [tabBarItems objectAtIndex:0];

---------> 在委托方法中获取项目操作

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger selectedTag = tabBar.selectedItem.tag;
    NSLog(@"%ld",(long)selectedTag);
    if (selectedTag == 0) {
        //Do what ever you want here
    } else if(selectedTag == 1) {
        //Do what ever you want
    } else { //if(selectedTag == 2)
        //Do what ever you want here
    }
}


更新:(Swift 2.0)

1. 第一个方面(UITabBarController):
这将允许我们将 UIViewController 配置为 UITabBarController 中的 tabbar 项。
下面是UITabBarController的基本方法:

--------->将 UITabBarController 添加到 appdelegate 中的窗口

let tabBarController = UITabBarController(nibName: nil, bundle: nil)
tabBarController.delegate = self // Need to import 'UITabBarControllerDelegate'
self.window!.rootViewController = tabBarController;
self.window?.makeKeyAndVisible();

--------->配置UIViewControllers

let firstViewController = UIViewController()
firstViewController.title = "FirstVC"
firstViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let firstNavController: UINavigationController = UINavigationController(rootViewController: firstViewController)

let secondViewController = UIViewController()
secondViewController.title = "SecondVC"
secondViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let secondNavController: UINavigationController = UINavigationController(rootViewController: secondViewController)

let thirdViewController = UIViewController()
thirdViewController.title = "ThirdVC"
thirdViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let thirdNavController: UINavigationController = UINavigationController(rootViewController: thirdViewController)

let forthViewController = UIViewController()
forthViewController.title = "ForthVC"
forthViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)
let forthNavController: UINavigationController = UINavigationController(rootViewController: forthViewController)

--------->将 UIViewController 附加到 UITabBarController

tabBarController.viewControllers = [firstNavController, secondNavController, thirdNavController, forthNavController]

--------->在“UITabBarControllerDelegate”中接收事件

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    print("didSelectViewController: \(viewController.title) ?")
}

2.第二个方面(UITabBar):
这将允许我们创建 tabbar 项目。我们可以在委托方法中获取单个项目的事件
以下是UITabBar的基本方法:

--------->将 UITabBar 添加到视图

let frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 49, CGRectGetWidth(self.view.frame), 49)
let tabBar: UITabBar = UITabBar(frame: frame)
tabBar.delegate = self // Need to import 'UITabBarDelegate'
self.view.addSubview(tabBar)

--------->准备 TabbarItems

let tabBarItem1 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let tabBarItem2 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let tabBarItem3 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let tabBarItem4 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)

--------->将UITabBarItems添加到UITabBar

tabBar.items = [tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4]
tabBar.selectedItem = tabBarItem1

--------->在“UITabBarDelegate”中接收事件

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    let selectedTag:Int = (tabBar.selectedItem?.tag)!
    print(selectedTag)
    switch selectedTag {
    case 0:
        print("tabBarItem1")
        //Do what ever you want here
    case 1:
        print("tabBarItem2")
        //Do what ever you want here
    case 2:
        print("tabBarItem3")
        //Do what ever you want here
    default:
        print("tabBarItem4")
        //Do what ever you want here
    }
}

Here the two aspects....

First Aspect (UITabBarController):
Here You can add multiple classes (i.e Viewcontrollers) to the UITabBarController as show in the previous answers...

Second Aspect (UITabbar):
Here You can add the Tabbar in any view controller...And for this Tabbar you can add multiple UITabBar Items..and You can get the individual Tabbar Item Action.....

See the Below Code For UITabBar

---------> Adding UITabBar

UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
myTabBar.delegate=self;   //here you need import the protocol <UITabBarDelegate>
[self.view addSubview:myTabBar];

---------> Adding Items to UITabBar

NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];

UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Item1" image:[UIImage imageNamed:@"Item1image.png"] tag:0];
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:1 ];
UITabBarItem *tabBarItem3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2 ];

[tabBarItems addObject:tabBarItem1];
[tabBarItems addObject:tabBarItem2];
[tabBarItems addObject:tabBarItem3];

myTabBar.items = tabBarItems;
myTabBar.selectedItem = [tabBarItems objectAtIndex:0];

---------> Getting The Item Actions in a Delegate Method

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSInteger selectedTag = tabBar.selectedItem.tag;
    NSLog(@"%ld",(long)selectedTag);
    if (selectedTag == 0) {
        //Do what ever you want here
    } else if(selectedTag == 1) {
        //Do what ever you want
    } else { //if(selectedTag == 2)
        //Do what ever you want here
    }
}


Update: (Swift 2.0)

1. First Aspect (UITabBarController):
This will allow us to configure UIViewControllers as tabbar items in UITabBarController.
Below is the basic approach for UITabBarController:

---------> Adding UITabBarController to window in appdelegate

let tabBarController = UITabBarController(nibName: nil, bundle: nil)
tabBarController.delegate = self // Need to import 'UITabBarControllerDelegate'
self.window!.rootViewController = tabBarController;
self.window?.makeKeyAndVisible();

---------> Configuring UIViewControllers

let firstViewController = UIViewController()
firstViewController.title = "FirstVC"
firstViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let firstNavController: UINavigationController = UINavigationController(rootViewController: firstViewController)

let secondViewController = UIViewController()
secondViewController.title = "SecondVC"
secondViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let secondNavController: UINavigationController = UINavigationController(rootViewController: secondViewController)

let thirdViewController = UIViewController()
thirdViewController.title = "ThirdVC"
thirdViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let thirdNavController: UINavigationController = UINavigationController(rootViewController: thirdViewController)

let forthViewController = UIViewController()
forthViewController.title = "ForthVC"
forthViewController.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)
let forthNavController: UINavigationController = UINavigationController(rootViewController: forthViewController)

---------> Attaching UIViewControllers to UITabBarController

tabBarController.viewControllers = [firstNavController, secondNavController, thirdNavController, forthNavController]

---------> Receiving event in 'UITabBarControllerDelegate'

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
    print("didSelectViewController: \(viewController.title) ?")
}

2. Second Aspect (UITabBar):
This will allow us to create tabbar items. We can get event for individual item in delegate method
Below is the basic approach for UITabBar:

---------> Adding UITabBar to a view

let frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - 49, CGRectGetWidth(self.view.frame), 49)
let tabBar: UITabBar = UITabBar(frame: frame)
tabBar.delegate = self // Need to import 'UITabBarDelegate'
self.view.addSubview(tabBar)

---------> Preparing TabbarItems

let tabBarItem1 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Bookmarks, tag: 0)
let tabBarItem2 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Contacts, tag: 1)
let tabBarItem3 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Downloads, tag: 2)
let tabBarItem4 = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.Favorites, tag: 3)

---------> Adding UITabBarItems to UITabBar

tabBar.items = [tabBarItem1, tabBarItem2, tabBarItem3, tabBarItem4]
tabBar.selectedItem = tabBarItem1

---------> Receiving event in 'UITabBarDelegate'

func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
    let selectedTag:Int = (tabBar.selectedItem?.tag)!
    print(selectedTag)
    switch selectedTag {
    case 0:
        print("tabBarItem1")
        //Do what ever you want here
    case 1:
        print("tabBarItem2")
        //Do what ever you want here
    case 2:
        print("tabBarItem3")
        //Do what ever you want here
    default:
        print("tabBarItem4")
        //Do what ever you want here
    }
}
吾性傲以野 2024-10-25 08:21:45
- (void) setUpTabBar {
    FirstViewController *firstViewController = [[FirstViewController alloc]init];
    firstViewController.title = @"First View";
    firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
    UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];

    SecondViewController *secondViewController = [[SecondViewController alloc]init];
    secondViewController.title = @"Second View";
    secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];

    ThirdViewController *thirdViewController = [[ThirdViewController alloc]init];
    thirdViewController.title = @"Third View";
    thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
    UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];

    ForthViewController *forthViewController = [[ForthViewController alloc]init];
    forthViewController.title = @"Forth View";
    forthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2];
    UINavigationController *forthNavController = [[UINavigationController alloc]initWithRootViewController:forthViewController];

    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
    tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, forthNavController, nil];
    tabBarController.delegate = self;             
    [self sizeViewToAvailableWindow:[tabBarController view]];

    [firstNavController release];
    [firstViewController release];

    [secondNavController release];
    [secondViewController release];

    [thirdNavController release];
    [thirdViewController release];

    [forthNavController release];
    [forthViewController release];
}

这是以编程方式制作 TabBarController 的代码。
希望这会对您有所帮助。

- (void) setUpTabBar {
    FirstViewController *firstViewController = [[FirstViewController alloc]init];
    firstViewController.title = @"First View";
    firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:0];
    UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];

    SecondViewController *secondViewController = [[SecondViewController alloc]init];
    secondViewController.title = @"Second View";
    secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];

    ThirdViewController *thirdViewController = [[ThirdViewController alloc]init];
    thirdViewController.title = @"Third View";
    thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:2];
    UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];

    ForthViewController *forthViewController = [[ForthViewController alloc]init];
    forthViewController.title = @"Forth View";
    forthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2];
    UINavigationController *forthNavController = [[UINavigationController alloc]initWithRootViewController:forthViewController];

    tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
    tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, forthNavController, nil];
    tabBarController.delegate = self;             
    [self sizeViewToAvailableWindow:[tabBarController view]];

    [firstNavController release];
    [firstViewController release];

    [secondNavController release];
    [secondViewController release];

    [thirdNavController release];
    [thirdViewController release];

    [forthNavController release];
    [forthViewController release];
}

Here is the code for making TabBarController programmatically.
Hope this will help you.

鹿港巷口少年归 2024-10-25 08:21:45

这就是你可以做的

    SubClassViewController1* vc1 = [[SubClassViewController1 alloc] init];
    UINavigationController* nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    nav1.navigationBar.hidden = YES;
    [vc1 release]; vc1 = nil;

    SubClassViewController2* vc2 = [[SubClassViewController2 alloc] init];
    vc2.title = @"List";
    UINavigationController* nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    nav2.navigationBar.hidden = YES;
    [vc2 release]; vc2 = nil;

    SubClassViewController3* vc3 = [[SubClassViewController3 alloc] init];
    vc3.title = @"Scan";
    UINavigationController* nav3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    nav3.navigationBar.hidden = YES;
    [vc3 release]; vc3 = nil;

    SubClassViewController4* vc4 = [[SubClassViewController4 alloc] init];
    vc4.title = @"Setting";
    UINavigationController* nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    nav4.navigationBar.hidden = YES;
    [vc4 release]; vc4 = nil;

    UITabBarController* tabBar = [[UITabBarController alloc] init];
    [tabBar setViewControllers:[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nil]];

    [self.navigationController pushViewController:tabBar animated:YES];
    [nav1 release]; nav1 = nil;
    [nav2 release]; nav2 = nil;
    [nav3 release]; nav3 = nil;
    [nav4 release]; nav4 = nil;
    [tabBar release]; tabBar = nil;

这将创建一个带有四个选项卡的选项卡栏,每个选项卡都有自己的视图控制器和导航控制器,即每个选项卡维护自己的堆栈来推送和弹出视图控制器。
我希望这对U有帮助。

here is what u can do

    SubClassViewController1* vc1 = [[SubClassViewController1 alloc] init];
    UINavigationController* nav1 = [[UINavigationController alloc] initWithRootViewController:vc1];
    nav1.navigationBar.hidden = YES;
    [vc1 release]; vc1 = nil;

    SubClassViewController2* vc2 = [[SubClassViewController2 alloc] init];
    vc2.title = @"List";
    UINavigationController* nav2 = [[UINavigationController alloc] initWithRootViewController:vc2];
    nav2.navigationBar.hidden = YES;
    [vc2 release]; vc2 = nil;

    SubClassViewController3* vc3 = [[SubClassViewController3 alloc] init];
    vc3.title = @"Scan";
    UINavigationController* nav3 = [[UINavigationController alloc] initWithRootViewController:vc3];
    nav3.navigationBar.hidden = YES;
    [vc3 release]; vc3 = nil;

    SubClassViewController4* vc4 = [[SubClassViewController4 alloc] init];
    vc4.title = @"Setting";
    UINavigationController* nav4 = [[UINavigationController alloc] initWithRootViewController:vc4];
    nav4.navigationBar.hidden = YES;
    [vc4 release]; vc4 = nil;

    UITabBarController* tabBar = [[UITabBarController alloc] init];
    [tabBar setViewControllers:[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nil]];

    [self.navigationController pushViewController:tabBar animated:YES];
    [nav1 release]; nav1 = nil;
    [nav2 release]; nav2 = nil;
    [nav3 release]; nav3 = nil;
    [nav4 release]; nav4 = nil;
    [tabBar release]; tabBar = nil;

This will create a tab bar with four tabs and each tab with its own viewcontroller and navigation controller, i.e each tab maintains its own stack to push and pop the view controller.
I hope that helps U.

记忆消瘦 2024-10-25 08:21:45

您可以执行以下操作:

UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
[self.view addSubview:tabBar];

要向其中添加一些项目,您可以编写:

NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage imageNamed:@"Image.png"] tag:0];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Title1" image:[UIImage imageNamed:@"Image1.png"] tag:1];
[tabBarItems addObject:tabBarItem];
[tabBarItems addObject:tabBarItem1];
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];

You can do something like:

UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 430, 320, 50)];
[self.view addSubview:tabBar];

To add some items to it, you can write:

NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage imageNamed:@"Image.png"] tag:0];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Title1" image:[UIImage imageNamed:@"Image1.png"] tag:1];
[tabBarItems addObject:tabBarItem];
[tabBarItems addObject:tabBarItem1];
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文