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

Iphone 将包含3项的选项卡栏添加到基于视图的应用程序,iphone,xcode,tabs,Iphone,Xcode,Tabs,所以,现在我已经创建了一个基于视图的应用程序,它有8个不同的视图。我希望它在3个视图上显示一个选项卡栏。此选项卡栏将有3个项目,允许用户切换到上述3个视图 我应该怎么做呢?非常感谢 AppDelegate.h @interface LoginPageAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> { UIWindow *window; LoginPageViewCon

所以,现在我已经创建了一个基于视图的应用程序,它有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
请求页面.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;
}

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

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

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

重新开始会很乏味。因为我在导航方面没有问题,所以我试图寻找一个快速解决这个问题的方法。不需要从头到脚重新开始。您只需将创建的视图复制并粘贴到新创建的基于tabbar的应用程序中,这样就不必费心管理这么多手动代码。无论如何,我该如何设置选项卡名称和图像?感谢您的及时回复。我不知道我做错了什么。我在这方面真的很新。我用我所做的编辑了我的问题,请看一看。应用程序在启动过程中崩溃。您更新了哪部分?我的解决方案是否适用于您,或者您的应用程序是否仍在崩溃?您缺少NSArray*控制器=[NSArray arrayWithObjects:searchNav、nearbyNav、mapNav、aboutUsNav、favoritesNav、nil];tabBarController.viewControllers=控制器;我用你的答案编辑了我的问题(如果我做得对的话)。
- (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;
}