使用 tabbed application 实现 tabbarcontroller 功能

xcode4.2(我使用的是xcode4.3)以及以上版本估计在实现tabview的时候不是很容易。

对于新手来说,完全按照书上来操作已经是不可能的了,因为xcode的版本不一样,导致模板,界面等很多东西都不同。

那今天这里就一步一步教大家如何在xcode4.2(或更高版本)来实现tabview

1.建立一个 tabbed application 工程。

注意,下面的3个选项都不要选。

2.这个时候 如果运行模拟器的话 可以看到如下的界面

3.我们暂时不想修改已经存在的这2个view ,而是在这个基础上添加一个view

通过 file-new- file...,选择 ios-Cocoa Touch - Objective-C class ,

在 class名字取为  AViewController,Subclass of 选择  UIViewController,然后选择“With XIB for user interface”完成。

4.打开 AViewController.m,修改如下函数

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

       //在这里添加tabbar item 的 title 以及image

      

        self.tabBarItem.title = @"Add One";

        self.tabBarItem.image = [UIImageimageNamed:@"first"];

    }

    returnself;

}

5.在 CRAppDelegate.m文件里面导入头文件

#import "AViewController.h"

修改函数如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    UIViewController *viewController1 = [[[CRFirstViewController alloc] initWithNibName:@"CRFirstViewController" bundle:nil] autorelease];

    UIViewController *viewController2 = [[[CRSecondViewController alloc] initWithNibName:@"CRSecondViewController" bundle:nil] autorelease];

    

    UIViewController *viewController3 = [[[AViewController alloc] initWithNibName:@"AViewController" bundle:nil] autorelease];

    //这里添加

    self.tabBarController = [[[UITabBarControlleralloc] init] autorelease];

    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3, nil];//这里添加

    self.window.rootViewController = self.tabBarController;

    [self.windowmakeKeyAndVisible];

    

    returnYES;

}

当然,还有其他的方法创建,详细的操作 请到这里查看http://my.oschina.net/u/586961/blog/66594  这样这个网页上的方法创建的时候需要勾选 "use storyboards"

原文地址:https://www.cnblogs.com/rollrock/p/2778045.html