IOS编程多视图应用程序完成后心得体会
使用Xcode的开发平台相对于使用c++ builder的开发平台来讲有几点不同:
1、Xcode建立一个window-base工程同CB中建立一个Forms Application工程相似,都会建立一个类class,作为主窗体,都自动生成3个文件;
2、Xcode创建的新窗体,是未与代码中的类链接,必须在身份检查器中修改File’s Owner的class默认为NSObject,将其改为代码中定义了的类,例如,BlueViewController。
例如:
双击BlueView.xib,单击File’s Owner图标,并按下⌘4打开身份检查器。File’s Owner的class默认为NSObject,将其改为BlueViewController。
图6-14 修改身份检查器
3、在Xcode编写代码比CB中编写代码更加方便,因为编写时会自动搜索相关连的代码,并显示出来供操作员选择,因此一个小小的应用其代码都很多,很长。
我学会了如何编写一个多视图的应用:
步骤:
1、新建工程
2、创建视图控制器和nib文件
(1)单击Groups & Files窗格中的Classes文件夹,然后按下⌘N或从File菜单中选择New File…从左侧窗格中选择Cocoa Touch Classes,可以看到用于常用的Cocoa Touch类的模板。选择UIViewController subclass。在右侧窗格中,可以看到一个标有With XIB for user interface的复选框。如果该复选框处于被选中状态,则单击它以取消选择。
单击Next,输入名称SwitchViewController.m确保选中了Also create “SwitchViewController.h”,然后单击Finish。
(2)创建两个nib文件,分别对应刚才创建的两个内容视图。单击Group&Files窗格中的Resource文件夹,以便在正确的位置创建它们。按下⌘N或从File菜单中选择New File…,在左侧窗格中的iPhone OS标题下选择User Interface ,选择View XIB模板的图标,这将创建一个带有内容视图的nib文件,然后单击Next按钮,输入BlueView.xib。
1. 修改应用程序委托
(1)单击Groups&Files窗格中的View_SwitchAppDelegate.h文件,并对文件进行修改:
#import <UIKit/UIKit.h>
@class SwitchViewController;
@interface View_SwitcherAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet SwitchViewController *switchViewController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) SwitchViewController *switchViewController;
@end
(2)修改View_SwitherAppDelegate.m添加代码将根控制器的视图添加到应用程序的主窗口:
#import “View_SwitherAppDelegate.h”
#import “SwitchViewController.h”
@synthesize window;
@synthesize switchViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:switchViewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[window release];
[switchViewController release];
[super dealloc];
}
2. 修改SwitchViewController.h
#import <UIKit/UIKit.h>
@class BlueViewController;
@class YellowViewController;
@interface SwitchViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
@property (retain, nonatomic) YellowViewController *yellowViewController;
@property (retain, nonatomic) BlueViewController *blueViewController;
-(IBAction)switchViews:(id)sender;
@end
3. 修改MainWindow.xib文件
(1)双击MainWindow.xib,在Interface Builder中打开它。从Library中添加一个视图控制器(View Controller)
图6-10 添加视图控制器
(2)在nib的主窗口中单击View Controller图标,并按下⌘4打开身份检查器:将其类修改为SwitchViewController
图6-11 身份检查器
(3)从库中拖一个View到SwitchViewController对应的窗口中。
图6-12 添加视图
(4)从库中拖动一个工具栏到视图上,将其放在底部。双击该工具栏的按钮将其标题改为Switch View,按住Ctrl键将该按钮拖到Switch View Controller图标,然后选择switchViews:操作。
图6-13 添加工具条
(5)按住Ctrl键并从View_SwicherAppDelegate图标拖到SwitchViewController图标,然后选择swicthViewController输出口。
4. 修改SwitchViewController.m
#import "SwitchViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"
@implementation SwitchViewController
@synthesize blueViewController;
@synthesize yellowViewController;
- (void)viewDidLoad
{
BlueViewController *blueController =
[[BlueViewController alloc] initWithNibName:@”BlueView” bundle:nil];
self.blueViewController = blueController;
[self.view insertSubview:blueController.view atIndex:0];
[blueController release];
}
- (IBAction)switchViews:(id)sender
{
if(self.yellowViewController.view.superview == nil)
{
if(self.yellowViewController == nil)
{
YellowViewController *yellowController =
[[YellowViewController alloc]
initWithNibName:@”YellowView” bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
}
else if(self.blueViewController == nil)
{
BlueViewController *blueController =
[[BlueViewController alloc] initWithNibName:@”BlueView” bundle:nil];
self.blueViewController = blueController;
[blueController release];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if (self.blueViewController.view.superview == nil)
self.blueViewController = nil;
else
self.yellowViewController = nil;
}
- (void)dealloc {
[yellowViewController release];
[blueViewController release];
[super dealloc];
}
5. 实现内容视图
(1)修改blueView内容视图控制器的声明文件:
#import <UIKit/UIKit.h>
@interface BlueViewController : UIViewController {
}
-(IBAction)blueButtonPressed:(id)sender;
@end
(2)修改yellowView视图控制器的声明文件:
#import <UIKit/UIKit.h>
@interface YellowViewController : UIViewController {
}
- (IBAction)yellowButtonPressed:(id)sender;
@end
(3)双击BlueView.xib,单击File’s Owner图标,并按下⌘4打开身份检查器。File’s Owner的class默认为NSObject,将其改为BlueViewController。
图6-14 修改身份检查器
(4)单击View图标,然后⌘1调出属性检查器。单击标有BackGround的颜色,将此视图的背景颜色设置为蓝色。
图6-15 修改视图背景颜色
(5)从库中拖出一个Round Rect Button到窗口上。双击该按钮,将其标题改为Press Me,将该按钮放到合适的地方。然后切换到连接检查器,将Touch Up Inside事件拖到File’s Owner图标,选择blueButtonPressed:操作方法。
图6-16 连接操作方法
(6)将BlueViewController的view输出口连接到nib中的视图。
图6-17 连接视图输出口
(7)双击YellowView.xib文件,对此Nib文件做同样的修改,将按钮标题修改为Press me,Too,背景颜色设置为黄色。
(8)打开BlueViewController.m文件实现操作方法
- (IBAction)blueButtonPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Blue View Button Pressed”
message:@”You pressed the button on the blue view”
delegate:nil
cancelButtonTitle:@”Yep, I did.”
otherButtonTitles:nil];
[alert show];
[alert release];
}
(9)打开YellowViewController.m文件实现操作方法
-(IBAction)yellowButtonPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@”Yellow View Button Pressed”
message:@”You pressed the button on the yellow view”
delegate:nil
cancelButtonTitle:@”Yep, I did.”
otherButtonTitles:nil];
[alert show];
[alert release];
}
6. 制作转换动作
(1)修改switchViews:操作方法。
- (IBAction)switchViews:(id)sender
{
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
if (self.yellowViewController.view.superview == nil)
{
if (self.yellowViewController == nil)
{
YellowViewController *yellowController =
[[YellowViewController alloc] initWithNibName:@"YellowView"
bundle:nil];
self.yellowViewController = yellowController;
[yellowController release];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.view cache:YES];
[blueViewController viewWillAppear:YES];
[yellowViewController viewWillDisappear:YES];
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
[yellowViewController viewDidDisappear:YES];
[blueViewController viewDidAppear:YES];
}
else
{
if (self.blueViewController == nil)
{
BlueViewController *blueController =
[[BlueViewController alloc] initWithNibName:@"BlueView"
bundle:nil];
self.blueViewController = blueController;
[blueController release];
}
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.view cache:YES];
[yellowViewController viewWillAppear:YES];
[blueViewController viewWillDisappear:YES];
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:blueViewController.view atIndex:0];
[blueViewController viewDidDisappear:YES];
[yellowViewController viewDidAppear:YES];
}
[UIView commitAnimations];
}
7. 运行程序,点击Press Me按钮。
图6-18 运行效果
8. 点击Switch View按钮,然后点击Press Me ,Too按钮。