我的iphone开发学习笔记(六): 使用选项卡栏管理多视图界面

今天练习的目标是:

1。 创建windows_based_application

2.    使用选项卡拦管理多视图界面

3。 各个视图之间的相互调用。


首先看下程序运行的结果。

   


主要的功能:

用户在三个视图中,分别计算矩形、圆形的面积,同时在summaryView统计所有计算的次数。


练习步骤:

1。 首先创建windows_based_application, 项目名称为TabConversion

2.  增加文件

      AreaViewController 及AreaView.xib(后者放到Resouces文件夹)

      VolumeViewController及VolumeView.xib(后者放到Resouces文件夹)

       SummaryViewController及SummaryView.xib(后者放到Resouces文件夹)

3. 打开MainWindow.xib

     添加TabViewController,

    在TabConversionAppDelegate添加UITabBarController的声明

    连接 UITabBarController和TabConversionAppDelegate

     

//
//  TabConversionAppDelegate.m
//  TabConversion
//
//  Created by 旭 陈 on 11-12-5.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TabConversionAppDelegate.h"

@implementation TabConversionAppDelegate

@synthesize window;
@synthesize tabBarController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    
    [window makeKeyAndVisible];
    //注意下面语句,将tabBarcontroller.view显示出来
    [window addSubview:tabBarController.view];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
    [window release];
    [super dealloc];
}


@end


  

4. 设置UITabBarController属性

     首先添加三个视图,分别是areaView, volumeView, summaryView

     然后选中视图,选择ClassIdentity, 分别选择AreaViewController, VolumeViewController, SummaryViewController;

     完成这几步后,就实现了Tabbar对于多视图的管理

      

   5。 接下来就是依次进行各个ViewController的界面设计,及控件和File's Owner的链接

          另外就是实现有关的面积的计算

          看下视图和代码,就很容易理解

           

            

//
//  AreaViewController.m
//  TabConversion
//
//  Created by 旭 陈 on 11-12-5.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "AreaViewController.h"


@implementation AreaViewController
@synthesize rectLength,rectWidth,rectArea;
@synthesize summaryViewController;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(IBAction)calculateArea:(id)sender{
	NSLog(@"caculateArea");
	float area = [rectLength.text floatValue]*[rectWidth.text floatValue];
	NSString *areaStr = [[NSString alloc]initWithFormat:@"%1.2f",area];
	rectArea.text = areaStr;
	
	[summaryViewController update];
}

-(IBAction)hideKeyboard:(id)sender{
	[rectLength resignFirstResponder];
	[rectWidth resignFirstResponder];
}

@end

这里有2个地方需要注意理解:

1。 hideKeyboard方法,就是调用resignFirstResponder, 来隐藏键盘

2。summaryViewController update方法,这个就是调用了另外视图的方法

      需要做的事情包括:a. 在areaViewController中导入SummaryViewController类

                                          b. 声明summaryViewController

                                          c.  选中AreaViewController, 按下controll,连线到SummaryViewController, 选择链接的变量

       


6. 剩余的工作比较简单,

    6。1 完成VolumeView.xib的界面设计和控件声明

    6。2 完成VolumeViewController的有关面积的计算

    6。3 完成SummaryView.xib的界面设计和控件声明、连接

   6。4 完成SummaryViewController的函数update, 全局变量updateTotal++


//
//  VolumeViewController.m
//  TabConversion
//
//  Created by 旭 陈 on 11-12-5.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "VolumeViewController.h"


@implementation VolumeViewController

@synthesize circleRadius,circleArea;
@synthesize summaryViewController;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(IBAction)doCaculate:(id)sender{
	NSLog(@"volume caculate");
	float myArea = [circleRadius.text floatValue]*[circleRadius.text floatValue]*3.1415;
	NSString *areaStr = [[NSString alloc]initWithFormat:@"%1.2f",myArea];
	circleArea.text = areaStr;
	
	[summaryViewController update];
}

-(IBAction)hideKeyboard:(id)sender{
	[circleRadius resignFirstResponder];
}

@end

//
//  SummaryViewController.m
//  TabConversion
//
//  Created by 旭 陈 on 11-12-5.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SummaryViewController.h"


@implementation SummaryViewController

@synthesize totalUpdate;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
-(void)viewDidAppear:(BOOL)animated{
	NSLog(@"viewDidApprear");
	NSString *result = [[NSString alloc]initWithFormat:@"%d",totalNum];
	totalUpdate.text = result;
	[result release];
	[super viewDidAppear:animated];
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

-(void)update{
	totalNum++;
	NSLog(@"%i",totalNum);
	
}


@end

这个例子完成,希望对大家有所帮助。

声明:

1。上述例子代码来源于书籍<<Iphone开发入门经典>>,经过本人的实际练习通过。

2。 需要本例子代码的童鞋,请留下电子邮件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值