如何创建一个Tab bar Application (xcode 4.2中或者代码的方式)
2011-10-09 11:36
393 查看
Xcode 4.2 缺省创建的Tabbed Application是这个样子的。
而且类被命名为XXFirstViewController,XXXSecondViewController,让我们很不爽,很多时候,我们需要更有意义的类名,更多的tab选项。那么我下面给大家演示以下如何自己定义这些。
我们假如创建一个有三个选项的工程,并且三个相关的viewcontroller分别是普通的,table View,带table view的导航模式的,并且命名为FViewController, SViewController,TViewController。
选择新建一个Tab bar Application,并把工程名称命名为tTabApp。
建好后的工程目录如下:
删除上面的tcFirstViewController.h,tcFirstViewController.m,tcSecondeViewController.h,tcSecondViewController.m,tcFirstViewController.xib,tcSecondViewController.xib六个文件。
在tTabApp上点击右键,加入FViewController.
选择UIViewController subclass
输入类名为FViewController,并且下面一定要选择Subclass of : UIViewController.
还需要选中With XIB for user interface.
然后依此假如另外两个View Controller,注意后面的两个必须在最后一个界面上的Subclass of : UITableViewController
这时候的工程目录如下:
在FViewController.m中修改一个函数
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//self.title = @"第一个";
self.tabBarItem.title =@"第一个";
self.tabBarItem.image = [UIImage
imageNamed:@"first"];
}
returnself;
}
修改SViewController中几个函数如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBarItem.title =@"第二个";
self.tabBarItem.image = [UIImage
imageNamed:@"second"];
}
returnself;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
删除上面的一样,这行永远会在编译期间生成一个警告
// Return the number of sections.
return 1;
上面一行0改为1
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static
NSString *CellIdentifier =
@"Cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==
nil) {
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier]autorelease];
}
cell.textLabel.text =@"1111";
上面这行是加入的,
// Configure the cell...
return cell;
}
在TViewController中做同样的更改,并且注意cell.textLabel.text = @"1111";改为cell.textLabel.text = @"222222";。以示区分。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title =
@"第三个";
self.tabBarItem.image = [UIImage
imageNamed:@"second"];
}
returnself;
}
在tcAppDelegate.m文件中加入import如下:
#import"FViewController.h"
#import"SViewController.h"
#import"TViewController.h"
在tcAppDelegate.m文件中修改函数
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FViewControlleralloc]initWithNibName:@"FViewController"bundle:nil];
UIViewController *viewController2 = [[SViewControlleralloc]initWithNibName:@"SViewController"bundle:nil];
UIViewController *viewController3 = [[TViewControlleralloc]initWithNibName:@"TViewController"bundle:nil];
UINavigationController *navigationController = [[UINavigationControlleralloc]initWithRootViewController:viewController3];
self.tabBarController = [[[UITabBarControlleralloc]init]autorelease];
self.tabBarController.viewControllers = [NSArrayarrayWithObjects:viewController1,
viewController2, navigationController,nil];
[viewController1
release];
[viewController2release];
[viewController3release];
[navigationControllerrelease];
self.window.rootViewController =self.tabBarController;
[self.windowmakeKeyAndVisible];
returnYES;
}
最后形成的效果是。
而且类被命名为XXFirstViewController,XXXSecondViewController,让我们很不爽,很多时候,我们需要更有意义的类名,更多的tab选项。那么我下面给大家演示以下如何自己定义这些。
我们假如创建一个有三个选项的工程,并且三个相关的viewcontroller分别是普通的,table View,带table view的导航模式的,并且命名为FViewController, SViewController,TViewController。
选择新建一个Tab bar Application,并把工程名称命名为tTabApp。
建好后的工程目录如下:
删除上面的tcFirstViewController.h,tcFirstViewController.m,tcSecondeViewController.h,tcSecondViewController.m,tcFirstViewController.xib,tcSecondViewController.xib六个文件。
在tTabApp上点击右键,加入FViewController.
选择UIViewController subclass
输入类名为FViewController,并且下面一定要选择Subclass of : UIViewController.
还需要选中With XIB for user interface.
然后依此假如另外两个View Controller,注意后面的两个必须在最后一个界面上的Subclass of : UITableViewController
这时候的工程目录如下:
在FViewController.m中修改一个函数
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//self.title = @"第一个";
self.tabBarItem.title =@"第一个";
self.tabBarItem.image = [UIImage
imageNamed:@"first"];
}
returnself;
}
修改SViewController中几个函数如下:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.tabBarItem.title =@"第二个";
self.tabBarItem.image = [UIImage
imageNamed:@"second"];
}
returnself;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
删除上面的一样,这行永远会在编译期间生成一个警告
// Return the number of sections.
return 1;
上面一行0改为1
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static
NSString *CellIdentifier =
@"Cell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell ==
nil) {
cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier]autorelease];
}
cell.textLabel.text =@"1111";
上面这行是加入的,
// Configure the cell...
return cell;
}
在TViewController中做同样的更改,并且注意cell.textLabel.text = @"1111";改为cell.textLabel.text = @"222222";。以示区分。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title =
@"第三个";
self.tabBarItem.image = [UIImage
imageNamed:@"second"];
}
returnself;
}
在tcAppDelegate.m文件中加入import如下:
#import"FViewController.h"
#import"SViewController.h"
#import"TViewController.h"
在tcAppDelegate.m文件中修改函数
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FViewControlleralloc]initWithNibName:@"FViewController"bundle:nil];
UIViewController *viewController2 = [[SViewControlleralloc]initWithNibName:@"SViewController"bundle:nil];
UIViewController *viewController3 = [[TViewControlleralloc]initWithNibName:@"TViewController"bundle:nil];
UINavigationController *navigationController = [[UINavigationControlleralloc]initWithRootViewController:viewController3];
self.tabBarController = [[[UITabBarControlleralloc]init]autorelease];
self.tabBarController.viewControllers = [NSArrayarrayWithObjects:viewController1,
viewController2, navigationController,nil];
[viewController1
release];
[viewController2release];
[viewController3release];
[navigationControllerrelease];
self.window.rootViewController =self.tabBarController;
[self.windowmakeKeyAndVisible];
returnYES;
}
最后形成的效果是。
相关文章推荐
- 如何创建一个Tab bar Application (xcode 4.0或4.1中的方式)
- UI中如何用纯代码的方式来实现一个图片轮播器
- 如何在桌面或者开始菜单中创建快捷方式
- Xcode 6 正式版如何创建一个Empty Application
- Xcode 6 正式版如何创建一个Empty Application
- 如何在一个工作站里创建多个工程(同一个xcode下面创建多个工程)
- Xcode 6 正式版如何创建一个Empty Application
- 如何用编程方式实现创建一个页面并替换掉站点首页
- webstorm如何创建一个代码模板?以创建vue代码模板为例
- Unity3D用代码方式创建一个Cube并对其进行操作用C#实现
- Xcode 6 正式版如何创建一个Empty Application
- 通过beego快速创建一个Restful风格API项目及API文档自动化 本文演示如何快速(一分钟内,不写一行代码)的根据数据库及表创建一个Restful风格的API项目,及提供便于在线测试API的界
- Xcode 7如何创建一个Empty Application的项目工程
- Xcode 6.1 正式版如何创建一个Empty Application
- Xcode 6以上版本如何创建一个空的工程(Empty Application)
- Xcode 6 正式版如何创建一个Empty Application
- 如何在macos下创建文件或者文件夹的快捷方式
- OL 2000: 如何以编程方式创建一个窗体 http://support.microsoft.com/kb/238755/zh-cn
- Unity5.1 创建IOS的Xcode项目. 如何手动增量更新应用的资源和代码.
- 下载时如何不弹出一个新窗口或者新Tab窗口