首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将包含3个项目的选项卡栏添加到基于视图的应用程序

将包含3个项目的选项卡栏添加到基于视图的应用程序
EN

Stack Overflow用户
提问于 2011-08-03 17:14:44
回答 2查看 915关注 0票数 2

所以,现在我已经创建了一个基于视图的应用程序,有8个不同的视图。我想让它在3个视图上显示一个选项卡栏。该选项卡栏将具有3个项目,这将允许用户切换到所述3个视图。

我该怎么做呢?非常感谢。

AppDelegate.h

代码语言:javascript
复制
@interface LoginPageAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    LoginPageViewController *viewController;
    UITabBarController *tabBarController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet LoginPageViewController *viewController;
@property (nonatomic, retain) IBOutlet IBOutlet UITabBarController *tabBarController;


@end

AppDelegate.m

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.delegate=self;

    RequestPage* requestPage = [[RequestPage alloc] init];  
    UIViewController *RequestPageView = [[UIViewController alloc] initWithRootViewController:requestPage];  

    StatusPage* statusPage = [[StatusPage alloc] init];  
    UIViewController *StatusPageView = [[UIViewController alloc] initWithRootViewController:statusPage];  
    NSArray* controllers = [NSArray arrayWithObjects:RequestPageView, StatusPageView, nil]; 
    tabBarController.viewControllers = controllers;

    [window addSubview:tabBarController.view];        

    [self.window makeKeyAndVisible];

    return YES;
}

RequestPage.m

代码语言:javascript
复制
- (id)init {
    self.title = @"Request Page";
    UIImage* anImage = [UIImage imageNamed:@"3.png"];
    UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Request Page" image:anImage tag:2];
    self.tabBarItem = theItem;
    [theItem release];
    return self;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-03 17:31:24

你需要从基于视图的应用程序开始。然后在appDelegate文件中创建一个UITabbarController

Appdelegate.h

代码语言:javascript
复制
UITabBarController *tabBarController;
// set properties

Appdelegate.m

代码语言:javascript
复制
// Synthsize

tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate=self;

//Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController  
Search * search = [[Search alloc] init];  
UINavigationController *searchNav = [[UINavigationController alloc] initWithRootViewController:search];  

Nearby* nearby = [[Nearby alloc] init];  
UINavigationController *nearbyNav = [[UINavigationController alloc] initWithRootViewController:nearby];  

Map* map = [[Map alloc] init];  
UINavigationController *mapNav = [[UINavigationController alloc] initWithRootViewController:map];  

AboutUs* aboutUs = [[AboutUs alloc] init];  
UINavigationController *aboutUsNav = [[UINavigationController alloc] initWithRootViewController:aboutUs];  

Favorites* favorites = [[Favorites alloc] init];  
UINavigationController *favoritesNav = [[UINavigationController alloc] initWithRootViewController:favorites];  

NSArray* controllers = [NSArray arrayWithObjects:searchNav,nearbyNav,mapNav,aboutUsNav,favoritesNav, nil];  
tabBarController.viewControllers = controllers;  

[window addSubview:tabBarController.view];    

您可以相应地管理要在哪个选项卡中放置导航控制器或仅放置视图控制器。

然后,在上面提到的每个视图控制器中,您需要实现

代码语言:javascript
复制
- (id)init {}

您可以在其中设置选项卡名和图像。

更新

代码语言:javascript
复制
- (id)init {
        self.title = @"Second";
        UIImage* anImage = [UIImage imageNamed:@"3.png"];
        UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Second" image:anImage tag:2];
        self.tabBarItem = theItem;
        [theItem release];
    return self;
}
票数 3
EN

Stack Overflow用户

发布于 2011-08-03 17:21:28

最好是创建一个基于标签栏的应用程序以及UINavigationController来导航多个视图。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6924141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档