《iPhone与iPad开发实战—iOS经典应用剖析》连载八

《iPhone与iPad开发实战—iOS经典应用剖析》连载八,第1张

概述3.3.5 主视图控制器代码 主视图控制器是MainViewController ,由于视图使用的控件都已经在视图MainView 中定义了,所以在视图控制器MainViewController 代码很少了,在本应用中还设计了按钮按下和按钮选择时候的普通和高亮状态效果。这些效果可以在检查器中设定,也可以通过代码设定,本应用是通过代码设定这些效果。 先看看主视图控制器类MainViewControl 3.3.5 主视图控制器代码

主视图控制器是MainVIEwController ,由于视图使用的控件都已经在视图MainVIEw 中定义了,所以在视图控制器MainVIEwController 代码很少了,在本应用中还设计了按钮按下和按钮选择时候的普通和高亮状态效果。这些效果可以在检查器中设定,也可以通过代码设定,本应用是通过代码设定这些效果。
先看看主视图控制器类MainVIEwController ,它的h 文件定义请参考“代码清单3-5Password/Classes/MainVIEwController.h ”所示。
【代码清单3-1】 Password/Classes/MainVIEwController.h
#import <UIKit/UIKit.h>@interface MainVIEwController : UIVIEwController {IBOutletUIbutton *createPassword;IBOutletUIbutton *emailPassword;}@property (nonatomic,retain) UIbutton*createPassword;@property (nonatomic,retain) UIbutton*emailPassword;@end


createPassword Generate Password 对应的控件变量,emailPassword E-Mail Password 对应的控件变量,它们都定义了输出口,这是因为它们都需要在程序中改变控件状态。
主视图控制器类MainVIEwController ,它的m 文件定义请参考“代码清单3-6Password/Classes/ MainVIEwController.m ”所示。
【代码清单3-2】 Password/Classes/ MainVIEwController.m
#import "MainVIEwController.h"#import "MainVIEw.h"@implementation MainVIEwController@synthesize createPassword;@synthesize emailPassword;- (ID)initWithNibname:(Nsstring *)nibnameOrNilbundle:(NSBundle *)nibBundleOrNil {if(self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]) {}returnself;}- (voID)vIEwDIDLoad {UIImage*buttonBackground = [[UIImage imagenamed:@"bluebutton.png"]stretchableImageWithleftCapWIDth:12.0 topCapHeight:12.0 ];[createPasswordsetBackgroundImage:buttonBackground forState:UIControlStatenormal];[emailPasswordsetBackgroundImage:buttonBackground forState:UIControlStatenormal];[createPasswordsetTitlecolor:[UIcolor whitecolor] forState:UIControlStatenormal];[emailPasswordsetTitlecolor:[UIcolor whitecolor] forState:UIControlStatenormal];UIImage*buttonBackgroundSel = [[UIImage imagenamed:@"whitebutton.png"]stretchableImageWithleftCapWIDth:12.0 topCapHeight:12.0 ];[createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateHighlighted];[createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateSelected];[emailPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateHighlighted];[emailPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateSelected];[createPasswordsetTitlecolor:[UIcolor blackcolor] forState:UIControlStateHighlighted];[createPasswordsetTitlecolor:[UIcolor blackcolor] forState:UIControlStateSelected];[emailPasswordsetTitlecolor:[UIcolor blackcolor] forState:UIControlStateHighlighted];[emailPasswordsetTitlecolor:[UIcolor blackcolor] forState:UIControlStateSelected];}- (BOol)shouldautorotatetoInterfaceOrIEntation:(UIInterfaceOrIEntation)interfaceOrIEntation{//Return YES for supported orIEntationsreturn(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 {[emailPasswordrelease];[createPasswordrelease];[superdealloc];}@end


其中vIEwDIDLoad 方法是我们讨论的重点,其中通过下面的方法定义了一个UIImage 对象:
UIImage *buttonBackground = [[UIImageimagenamed:@"bluebutton.png"] stretchableImageWithleftCapWIDth:12.0topCapHeight:12.0 ];
该方法是通过拉伸创建一个UIImage ,而边角不拉伸,需要两个参数,第一个是不拉伸区域和左边框的宽度,第二个参数是不拉伸区域和上边框的宽度。
把这个拉伸的UIImage 对象作为两个按钮的正常状态时候背景图片:
[createPassword setBackgroundImage:buttonBackgroundforState:UIControlStatenormal];
[emailPassword setBackgroundImage:buttonBackgroundforState:UIControlStatenormal];
接下来又定义了一个UIImage 对象,作为按钮其它状态(高亮状态UIControlStateHighlighted 和选中状态UIControlStateSelected )时候的背景图片,
[createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateHighlighted];
[createPasswordsetBackgroundImage:buttonBackgroundSel forState:UIControlStateSelected];
然后,又定义了按钮在高亮状态和选中状态时候的文字背景颜色:
[createPassword setTitlecolor:[UIcolor blackcolor]forState:UIControlStateHighlighted];
[createPassword setTitlecolor:[UIcolor blackcolor]forState:UIControlStateSelected];

3.3.6 背后视图UI
背后面视图如图3-43 所示是FlipsIDeVIEw (背后视图)设计窗口,我们一步一步介绍如何实现该视图设计和编程。




3-43 FlipsIDeVIEw 视图设计窗口
背后视图中的控件进行了编号,视图中的控件内容见表3-8 所示。
3-8 FlipsIDeVIEw 视图中的控件
                                    
  编号
  
  控件项目
  
  控件类型
  
  1
  
  Check out our other applications.
  
  UILabel
  
  2
  
  图片控件
  
  UIImageVIEw
  
  3
  
  iFlame is a virtual lighter…
  
  UITextVIEw
  
  4
  
  Download
  
  UIbutton
  
  5
  
  Don't be caught without a light…
  
  UITextVIEw
  
[color=windowtext -moz-use-text-color] 从图3-42 可以看到这些控件不包含如图3-44 所示导航栏和Done ,导航栏和Done 不用在FlipsIDeVIEw.xib 文件中设计好,而是通过程序代码动态添加的,代码是在RootVIEwController.m loadFlipsIDeVIEwController 方法实现的。



3-44 FlipsIDeVIEw 视图中的导航栏
我们可以按照表3-8 一一添加这些控制,需要注意的是1 号控件是UILabel ,而3 5 号控件是UITextVIEw ,当有很多的文本内容需要显示的时候就要使用UITextVIEw 控件而不是UILabel 。这个视图设计过程细节就不再一一介绍了。
  3.6.7 背后面视图和视图控制器代码
背后视图主要实现了2 个功能:导航栏中的Done 按钮和视图中Download 按钮,其中导航栏中的Done 功能的实现是在RootVIEwController.m 类的toggleVIEw 方法中已经实现了,而不是在FlipsIDeVIEw.m FlipsIDeVIEwController.m 中实现的。
Download 按钮是通过浏览器打开在App Store 上一个iFlame 应用,该功能是在FlipsIDeVIEw.m 中实现的。
我们先看看FlipsIDeVIEw.h 代码请参考“代码清单3-7 Password/Classes/ FlipsIDeVIEw.h ”所示。
【代码清单3-1】 Password/Classes/ FlipsIDeVIEw.h
#import <UIKit/UIKit.h>

@interface FlipsIDeVIEw : UIVIEw {
}

- (IBAction)openlink;

@end
其中的openlink 方法是响应Download 按钮事件。FlipsIDeVIEw.m 代码请参考“代码清单3-8 Password/Classes/ FlipsIDeVIEw.m ”所示。
【代码清单3-2】 Password/Classes/ FlipsIDeVIEw.m
#import "FlipsIDeVIEw.h"@implementation FlipsIDeVIEw- (ID)initWithFrame:(CGRect)frame {if(self = [super initWithFrame:frame]) {//Initialization code}returnself;}- (voID)drawRect:(CGRect)rect {//Drawing code}- (voID)dealloc {[superdealloc];}-(IBAction) openlink {//open in Safari//[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"http://www.apple.com/"]];[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/vIEwSoftware?ID=287545019&mt=8"]];}@end


上面的代码主要的方法是openlink ,通过该方法在iOS 浏览器中打开一个网页。其中使用[UIApplication sharedApplication] openURL: 方法,该方法介绍E-Mail 发送功能时候已经介绍了,它可以打开多种应用程序。
本章小结
通过对本章的学习,读者可以掌握密码生成应用程序(Amuck Password Generator )应用开发过程,重点是一些基本控件设计和使用过程,这些控件包括:UIVIEw UIbutton UILabel 等,学会使用Interface Builder ,在Interface Builder 设计这些控件,设定它们的属性。
读者还可以了解MVC 设计模式、实用型应用程序模板等概念,Cocoa Cocoa touch MVC 设计模式最为重要的设计模式,只有能够真正的理解好MVC 设计模式,才能做好iOS 开发,才能理解nib 文件、视图和视图控制器这些概念。UIVIEw 级别动画是iOS 比较简单但很常用的动画,UIVIEw 级别动画必须放在[UIVIEw beginAnimations:nilcontext:NulL] [UIVIEwcommitAnimations] 语句之间,其中包括了设定动画持续时间、动画转变类型和动画曲线等动画属性的设定。
此外,读者还可以掌握[UIApplication sharedApplication] openURL: 方法的使用,iOS 中这个[UIApplication sharedApplication]openURL 方法可以做很多事情,其中包括:打开浏览器、打开Google 地图、拨打电话、发送短信和发送Email 等等。 总结

以上是内存溢出为你收集整理的《iPhone与iPad开发实战—iOS经典应用剖析》连载八全部内容,希望文章能够帮你解决《iPhone与iPad开发实战—iOS经典应用剖析》连载八所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1090906.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存