《iPhone与iPad开发实战—iOS经典应用剖析》连载五
RootViewController的h文件编写完成我们接着编写RootViewController的m文件如“代码清单3-2 Password/Classes/RootViewController.m”所示。
【代码清单3-1】 Password/Classes/RootViewController.m
1 #import "RootViewController.h" 2 #import "MainViewController.h" 3 #import "FlipsideViewController.h" 4 5 6 @implementation RootViewController 7 8 @synthesize infoButton; 9 @synthesize flipsideNavigationBar; 10 @synthesize mainViewController; 11 @synthesize flipsideViewController; 12 13 14 - (void)viewDidLoad { 15 16 MainViewController*viewController = [[MainViewController alloc]initWithNibName:@"MainView" bundle:nil]; 17 self.mainViewController= viewController; 18 [viewControllerrelease]; 19 20 [self.viewinsertSubview:mainViewController.view belowSubview:infoButton]; 21 } 22 23 24 - (void)loadFlipsideViewController { 25 26 FlipsideViewController*viewController = [[FlipsideViewController alloc]initWithNibName:@"FlipsideView" bundle:nil]; 27 self.flipsideViewController= viewController; 28 [viewControllerrelease]; 29 30 //Set up the navigation bar 31 UINavigationBar*aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0,320.0, 44.0)]; 32 aNavigationBar.barStyle= UIBarStyleBlackOpaque; 33 self.flipsideNavigationBar= aNavigationBar; 34 [aNavigationBarrelease]; 35 36 UIBarButtonItem*buttonItem = [[UIBarButtonItem alloc] 37 initWithBarButtonSystemItem:UIBarButtonSystemItemDone 38 target:selfaction:@selector(toggleView)]; 39 UINavigationItem*navigationItem = [[UINavigationItem alloc] initWithTitle:@"PasswordGenerator"]; 40 navigationItem.rightBarButtonItem= buttonItem; 41 [flipsideNavigationBarpushNavigationItem:navigationItem animated:NO]; 42 [navigationItemrelease]; 43 [buttonItemrelease]; 44 } 45 46 47 - (IBAction)toggleView { 48 /* 49 This method is called when the info or Donebutton is pressed. 50 It flips the displayed view from the main viewto the flipside view and vice-versa. 51 */ 52 if(flipsideViewController == nil) { 53 [selfloadFlipsideViewController]; 54 } 55 56 UIView*mainView = mainViewController.view; 57 UIView*flipsideView = flipsideViewController.view; 58 59 [UIViewbeginAnimations:nil context:NULL]; 60 [UIViewsetAnimationDuration:1]; 61 [UIViewsetAnimationTransition:([mainView superview] 62 ?UIViewAnimationTransitionFlipFromRight 63 : UIViewAnimationTransitionFlipFromLeft)forView:self.view cache:YES]; 64 65 if([mainView superview] != nil) { 66 [flipsideViewControllerviewWillAppear:YES]; 67 [mainViewControllerviewWillDisappear:YES]; 68 [mainViewremoveFromSuperview]; 69 [infoButton removeFromSuperview]; 70 [self.viewaddSubview:flipsideView]; 71 [self.viewinsertSubview:flipsideNavigationBar aboveSubview:flipsideView]; 72 [mainViewControllerviewDidDisappear:YES]; 73 [flipsideViewControllerviewDidAppear:YES]; 74 75 }else { 76 [mainViewControllerviewWillAppear:YES]; 77 [flipsideViewControllerviewWillDisappear:YES]; 78 [flipsideViewremoveFromSuperview]; 79 [flipsideNavigationBarremoveFromSuperview]; 80 [self.viewaddSubview:mainView]; 81 [self.viewinsertSubview:infoButton aboveSubview:mainViewController.view]; 82 [flipsideViewControllerviewDidDisappear:YES]; 83 [mainViewControllerviewDidAppear:YES]; 84 } 85 [UIViewcommitAnimations]; 86 } 87 88 89 90 -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 91 //Return YES for supported orientations 92 return(interfaceOrientation == UIInterfaceOrientationPortrait); 93 } 94 95 96 - (void)didReceiveMemoryWarning { 97 [superdidReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 98 //Release anything that's not essential, such as cached data 99 } 100 101 102 - (void)dealloc { 103 [infoButtonrelease]; 104 [flipsideNavigationBarrelease]; 105 [mainViewControllerrelease]; 106 [flipsideViewControllerrelease]; 107 [superdealloc]; 108 } 109 110 111 @end
m文件中方法很多,值得我们关注有3个:viewDidLoad、loadFlipsideViewController和toggleView,下分别介绍这个3个方法。
viewDidLoad是视图加载完成还没有显示时候回调的方法,在本应用中该方法是创建一个主视图控制器,下面语句就是从一个给定nib文件名字创建一个控制器的通用方法:
MainViewController *viewController =[[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
主视图控制器创建完成需要把指针赋值给本类的mainViewController属性,因为该属性设置retain,这样可以防止内存泄漏,接着再释放刚才创建的viewController对象。
self.mainViewController= viewController;
[viewControllerrelease];
在viewDidLoad该方法最后,通过下面的语句实现,将创建的主视图控制器中的视图放入到当前视图中作为子视图,并且位于按钮下面,在iOS中视图可以有子视图,子视图摆放是有顺序的,前后顺序的不同会引起遮挡和影响事件响应。
[self.view insertSubview:mainViewController.viewbelowSubview:infoButton];
与– insertSubview:aboveSubview:类似的方法还有:
· –addSubview:,直接在前面增加子视图;
· –insertSubview:atIndex:,按照索引插入子视图;
· –exchangeSubviewAtIndex:withSubviewAtIndex:,交换两个子视图的顺序,常用于视图切换。
loadFlipsideViewController方法主要作用就是创建背后视图和上面的导航栏,创建背后视图控制器方法与主视图控制器方法一样,这里不再多说了。在iOS中导航栏比较麻烦的控件,它涉及到导航栏(Navigation Bar)、导航项目(Navigation Item)和导航按钮(Bar Button Item),在导航栏中包含导航项目,导航项目包含导航按钮,导航项目可以有标题,导航按钮可以有左右两个按钮,它们的关系如图3-35所示。
图3-35导航栏、导航项目和导航按钮的关系
因此我们需要创建这3个对象,首先创建导航栏使用下面的语句:
UINavigationBar *aNavigationBar = [[UINavigationBaralloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
使用该方法可以通过指定一个矩形轮廓来创建一个导航栏,创建完成导航栏后还要指定它的样式:
aNavigationBar.barStyle= UIBarStyleBlackOpaque;
创建完成导航栏接着创建导航按钮,然后再创建导航项目,再把导航按钮放到导航项目中。下面语句是创建一个导航按钮:
UIBarButtonItem *buttonItem = [[UIBarButtonItemalloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:selfaction:@selector(toggleView)];
在该语句中指定了按钮的样式,同时指定了按钮点击事件触发的时候调用的方法toggleView。导航项目创建是通过下面语句实现的:
UINavigationItem *navigationItem =[[UINavigationItem alloc] initWithTitle:@"Password Generator"];
在该语句中通过指定标题来构造一个导航项目,然后再通过下面的语句把刚才创建的导航按钮放在到导航项目中,作为它的右按钮。
navigationItem.rightBarButtonItem = buttonItem;
最后还要把导航项目放入到导航栏中,这个目标是通过下面语句实现的,并且要求动画显示:
[flipsideNavigationBar pushNavigationItem:navigationItemanimated:NO];
导航栏维护一个堆栈,通过发出pushNavigationItem:animated:消息把一个导航项目压栈,发出popNavigationItemAnimated:消息把一个导航项目出栈,当前视图显示的是栈顶的导航项目,所以,压栈可以进入下一级导航项目,而出栈可以返回以上一级导航项目。
toggleView方法是点击主视图的按钮和背后视图的Done按钮时候触发的方法,在该方法中实现了两个视图的切换。在该方法中主要涉及到两个知识点,一个是UIView切换问题,另一个是UIView动画问题。两个问题是相伴而生的,在UIView切换的时候往往伴随着动画发生。下面我们先看看UIView切换问题。
在视图切换有很多种方式,可以采用模态视图控制器切换、导航控制器切换和普通视图控制器切换。在本应用中采用的是普通视图控制器却换,它是定义一个根控制器,通过根控制器来控制其它视图交替切换。RootViewController就是视图控制器,其中有自己的View对象(根视图),当主视图加载时候,将主视图作为根视图的子视图放入,当主视图向背后面翻转的时候,把主视图从根视图中移除掉,把背后视图作为根视图的子视图放入。当背后面向主视图翻转的时候,把背后视图从根视图中移除掉,把主视图作为根视图的子视图放入,这样反反复复。
采用模态视图控制器可以参考苹果官方文档http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html。
下面我们看看toggleView方法的代码,该方法首先执行下面的语句,实现初始化背后视图控制器目的,这里有一个判断如果flipsideViewController为nil时候才去发出loadFlipsideViewController消息,可以防止多重加载。
if(flipsideViewController == nil) {
[selfloadFlipsideViewController];
}
接下来通过下面语句从视图控制器其中获得主视图和背后视图,由于在viewDidLoad方法中创建了主视图控制器,在loadFlipsideViewController方法中创建了背后视图控制器,因此在这里获得的视图对象不会是nil的。
UIView*mainView = mainViewController.view;
UIView*flipsideView = flipsideViewController.view;
在iOS中动画有多种形式,这里的视图翻转是属于UIView级别动画,UIView级别动画必须从[UIView beginAnimations:nilcontext:NULL]开始到[UIViewcommitAnimations]结束。在本应用中与UIView级别动画有关代码如下:
[UIViewbeginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:1];
[UIViewsetAnimationTransition:([mainView superview]
?UIViewAnimationTransitionFlipFromRight
: UIViewAnimationTransitionFlipFromLeft)forView:self.view cache:YES];
… …
[UIViewcommitAnimations];
setAnimationDuration:语句设置动画持续时间,setAnimationTransition:forView: cache:方法中第一个参数定义动画转变类型,第二个参数是当前视图对象,第三个参数是是否使用缓冲区。动画转变类型是指动画样式,其取值是UIViewAnimationTransition枚举类型,UIViewAnimationTransition的成员有:
· UIViewAnimationTransitionNone,不指定过渡类型;
· UIViewAnimationTransitionFlipFromLeft,指定从左侧翻转;
· UIViewAnimationTransitionFlipFromRight,指定从右侧翻转;
· UIViewAnimationTransitionCurlUp,指定向上卷起;
· UIViewAnimationTransitionCurlDown,指定向下卷起。
虽然,在本应用中没有使用,但是UIView级别动画中还有一个重要方法setAnimationCurve:,该方法可以设置动画曲线,动画曲线是决定动画进入和退出屏幕的方式。其它取值是UIViewAnimationCurve枚举类型,UIViewAnimationCurve的成员有:
· UIViewAnimationCurveEaseInOut,淡入淡出,开始时候慢,慢变快,中间最快,然后变慢;
· UIViewAnimationCurveEaseIn,淡入,开始时候慢然后越来越快;
· UIViewAnimationCurveEaseOut,淡出,开始快然后越来越慢;
· UIViewAnimationCurveLinear,线性匀速,开始和结束是一个速度。
判断语句if ([mainView superview] != nil) {}可以判断当前的视图是否为主视图,因为只有当前视图是主视图情况下主视图才有父视图,否则就是当前视图就是背后视图。如果当前视图是主视图时候代码如下:
[flipsideViewControllerviewWillAppear:YES];
[mainViewControllerviewWillDisappear:YES];
[mainViewremoveFromSuperview];
[infoButton removeFromSuperview];
[self.viewaddSubview:flipsideView];
[self.viewinsertSubview:flipsideNavigationBar aboveSubview:flipsideView];
[mainViewControllerviewDidDisappear:YES];
[flipsideViewControllerviewDidAppear:YES];
上面的这段代码就是要实现从主视图向背后面翻转,因此,需要在根视图中移除主视图[mainView removeFromSuperview],同时还移除了infoButton 对象[infoButton removeFromSuperview],它们两个都是在主视图上显示的。 [self.viewaddSubview:flipsideView]方法把背后视图添加到当前视图。在通过 [self.viewinsertSubview:flipsideNavigationBar aboveSubview:flipsideView]语句把导航栏添加到背后视图上面,使得导航栏与背后视图同是当前视图的子视图,只不过是导航栏在前面。
此外,代码中还用到了控制器中视图出现和消失的几个事件方法:
· viewWillAppear:,通知视图控制器,它的视图将要可见;
· viewWillDisappear:,通知视图控制器,它的视图将要消失;
· viewDidDisappear:,通知视图控制器,它的视图已经消失;
· viewDidAppear:,通知视图控制器,它的视图已经可见。
事实上,这几个方法经常在视图控制器中被重写,用于处理视图的不同生命周期中会触发的事件,它们与viewDidLoad和viewDidUnload事件类似,它们调用会更加频繁触发。其中viewDidLoad和viewWillAppear:很类似,viewDidLoad是视图被创建时候触发,接着视图变成可见的时候会触发viewWillAppear:事件,当视图变换到别的视图,视图从可见变成不可见,再次回来时候,视图从不可见变成可见viewDidLoad方法就不会触发,而viewWillAppear:事件会触发。
#import "RootViewController.h"
#import "MainViewController.h"
#import "FlipsideViewController.h"
@implementation RootViewController
@synthesize infoButton;
@synthesize flipsideNavigationBar;
@synthesize mainViewController;
@synthesize flipsideViewController;
- (void)viewDidLoad {
MainViewController*viewController = [[MainViewController alloc]initWithNibName:@"MainView" bundle:nil];
self.mainViewController= viewController;
[viewControllerrelease];
[self.viewinsertSubview:mainViewController.view belowSubview:infoButton];
}
- (void)loadFlipsideViewController {
FlipsideViewController*viewController = [[FlipsideViewController alloc]initWithNibName:@"FlipsideView" bundle:nil];
self.flipsideViewController= viewController;
[viewControllerrelease];
//Set up the navigation bar
UINavigationBar*aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0,320.0, 44.0)];
aNavigationBar.barStyle= UIBarStyleBlackOpaque;
self.flipsideNavigationBar= aNavigationBar;
[aNavigationBarrelease];
UIBarButtonItem*buttonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:selfaction:@selector(toggleView)];
UINavigationItem*navigationItem = [[UINavigationItem alloc] initWithTitle:@"PasswordGenerator"];
navigationItem.rightBarButtonItem= buttonItem;
[flipsideNavigationBarpushNavigationItem:navigationItem animated:NO];
[navigationItemrelease];
[buttonItemrelease];
}
- (IBAction)toggleView {
/*
This method is called when the info or Donebutton is pressed.
It flips the displayed view from the main viewto the flipside view and vice-versa.
*/
if(flipsideViewController == nil) {
[selfloadFlipsideViewController];
}
UIView*mainView = mainViewController.view;
UIView*flipsideView = flipsideViewController.view;
[UIViewbeginAnimations:nil context:NULL];
[UIViewsetAnimationDuration:1];
[UIViewsetAnimationTransition:([mainView superview]
?UIViewAnimationTransitionFlipFromRight
: UIViewAnimationTransitionFlipFromLeft)forView:self.view cache:YES];
if([mainView superview] != nil) {
[flipsideViewControllerviewWillAppear:YES];
[mainViewControllerviewWillDisappear:YES];
[mainViewremoveFromSuperview];
[infoButton removeFromSuperview];
[self.viewaddSubview:flipsideView];
[self.viewinsertSubview:flipsideNavigationBar aboveSubview:flipsideView];
[mainViewControllerviewDidDisappear:YES];
[flipsideViewControllerviewDidAppear:YES];
}else {
[mainViewControllerviewWillAppear:YES];
[flipsideViewControllerviewWillDisappear:YES];
[flipsideViewremoveFromSuperview];
[flipsideNavigationBarremoveFromSuperview];
[self.viewaddSubview:mainView];
[self.viewinsertSubview:infoButton aboveSubview:mainViewController.view];
[flipsideViewControllerviewDidDisappear:YES];
[mainViewControllerviewDidAppear:YES];
}
[UIViewcommitAnimations];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
//Return YES for supported orientations
return(interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
//Release anything that's not essential, such as cached data
}
- (void)dealloc {
[infoButtonrelease];
[flipsideNavigationBarrelease];
[mainViewControllerrelease];
[flipsideViewControllerrelease];
[superdealloc];
}
@end