博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《iPhone与iPad开发实战—iOS经典应用剖析》连载八
阅读量:6876 次
发布时间:2019-06-26

本文共 5844 字,大约阅读时间需要 19 分钟。

3.3.5 主视图控制器代码
主视图控制器是MainViewController
,由于视图使用的控件都已经在视图MainView
中定义了,所以在视图控制器MainViewController
代码很少了,在本应用中还设计了按钮按下和按钮选择时候的普通和高亮状态效果。这些效果可以在检查器中设定,也可以通过代码设定,本应用是通过代码设定这些效果。
先看看主视图控制器类MainViewController
,它的h
文件定义请参考“代码清单3-5Password/Classes/MainViewController.h
”所示。
【代码清单3-1】 Password/Classes/MainViewController.h
#
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 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 {
[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
(背后视图)设计窗口,我们一步一步介绍如何实现该视图设计和编程。
8_47717_880e40636b3d21c.jpg
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
  
从图3-42
可以看到这些控件不包含如图3-44
所示导航栏和Done
,导航栏和Done
不用在FlipsideView.xib
文件中设计好,而是通过程序代码动态添加的,代码是在RootViewController.m
loadFlipsideViewController
方法实现的。
8_47717_e4f15758dc3b91e.jpg
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
等等。

转载地址:http://gbofl.baihongyu.com/

你可能感兴趣的文章
PLSQL子程序即PLSQL块
查看>>
exportfs+NFS客户端问题
查看>>
检测主机是否存活的脚本
查看>>
Linux LVM之快照
查看>>
flask学习总结
查看>>
apr_socket_recv: Connection reset by peer (104)
查看>>
JAVA环境搭建
查看>>
Linux运维 第二阶段 (三)软件安装
查看>>
Exchange 2016/2013 - 禁用OWA密码更改
查看>>
初识CSS预处理器
查看>>
SQL Server Profiler和数据库引擎优化顾问
查看>>
启用ashx的session
查看>>
Nginx里的location以及如何用户认证
查看>>
RHEL7/CENTOS7忘记root密码的修改方式
查看>>
如何将邮件投递到本地?
查看>>
Android TextView字体设置
查看>>
线程池自定义扩展,捕获异常位置(非常有用)
查看>>
Uber将推电商快递服务 第一站为纽约
查看>>
Redis学习到实战(二)linux安装与从主
查看>>
Java代码风格格式化和检查配置
查看>>