将带有3个项目的标签栏添加到基于视图的应用程序

时间:2011-08-03 09:14:44

标签: iphone xcode tabs

所以,现在我已经创建了一个包含8个不同视图的基于视图的应用程序。我希望它在3个视图上显示一个标签栏。此标签栏将有3个项目,这将允许用户切换到3个所述视图。

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

AppDelegate.h

@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

- (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

- (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;
}

2 个答案:

答案 0 :(得分:3)

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

Appdelegate.h

UITabBarController *tabBarController;
// set properties

Appdelegate.m

// 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];    

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

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

- (id)init {}

您可以在其中设置标签名称和图像。

更新

- (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;
}

答案 1 :(得分:1)

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