iPhone SDK开发基础之iPhone程序框架

书以严密的体系性提供了iPhone和iPad软件开发从入门到专家的系统性知识,并提供来源于真实项目的可重用商业代码。书中的每个实例都是项目经验的提炼,深入浅出地讲解iPhone和iPad软件开发的核心技术要点,基本涵盖了iOS软件开发在真实商业项目中所需要的所有主题,并将实例介绍的技术深度和超值的实用性结合在一起,成为本书的特色。 


 iOS样章.rar (6622 K) 下载次数:372 

下面为大家连载此书部分章节供大家讨论。 


                                             iPhone SDK开发基础之iPhone程序框架 
    总的来说iPhone程序有两类框架,一类是游戏框架,另一类是非游戏框架,这里介绍的是非游戏框架,即基于iPhone 用户界面标准控件的程序框架。 
典型的iPhone程序包含一个Window和几个UIViewController,每个UIViewController管理多个UIView(可能是UITableView、UIWebView、UIImageView等),如图3-24所示。这些UIView之间如何进行层次迭放、显示、隐藏、旋转、移动等都由UIViewController进行管理,而UIViewController之间的切换,通常情况是通过UINavigationController、UITabBarController或UISplitViewController进行切换。接下来笔者会逐一介绍如何使用这三种Controller来切换你的UIViewController,以及在UIViewController中如何组织和管理你的各种UIView。 





图3-24 iPhone程序框架示意图 



                         iPhone SDK开发基础之   OpenFlow编程 

    当用户界面需要按页面显示图片时,使用OpenFlow库提供的功能,将要显示的用户界面图片分页进行显示会使编程工作变得非常快捷。该库提供了与OS X桌面Finder程序相同的视觉效果,如图3-46所示就是一个使用OpenFlow库逐页进行图片显示的程序,用户按下屏幕即可进行左右滚动显示,双击图片即可对当前显示的图片进行选取操作。 

图3-46 OpenFlow编程实例界面 
程序在视图控制器的viewDidAppear()中使用refreshCoverFlow()函数初始化OpenFlow库,通过setNumberOfImages()函数设置图片数量,代码如下。 
// RootViewController.m 
- (void)refreshCoverFlow{ 

CGRect bounds = [[UIScreen mainScreen] bounds]; 
AFOpenFlowView *coverFlowView = (AFOpenFlowView*)[self.view viewWithTag:kTagCoverflow]; 
if(coverFlowView != nil) 
[coverFlowView removeFromSuperview]; 
coverFlowView = [[AFOpenFlowView alloc] initWithFrame:CGRectMake(0, -30, bounds.size.width, COVERFLOWHEIGHT)]; 
coverFlowView.dataSource = self; 
coverFlowView.viewDelegate = self; 
coverFlowView.defaultImage = [self defaultImage]; 
coverFlowView.tag = kTagCoverflow; 
[self.view addSubview:coverFlowView]; 


NSInteger count = [self numberOfAnimals]; 
[coverFlowView setNumberOfImages:count]; 
//... 

[coverFlowView release]; 


并在loadView()中初始化图片,将图片从资源中加载并保存在一个NSMutableArray类型的变量imageArray中,代码如下。 
- (BOOL)doAddAnimal:(NSString *)name Image:(NSString *)imageName{ 

UIImage *image = [UIImage imageNamed: imageName]; 
if(image == nil) return FALSE; 
CGSize size = CGSizeMake(179, 208); 
[imageArray addObject:[self resizeImage:image scaledToSize:size]]; 
return TRUE; 

在OpenFlow库的requestImageForIndex delegate方法中直接通过NSMutableArray的索引作为OpenFlow库的图片索引,并通过该索引设置和获取具体图片,代码如下。 
// PageViewController.m 
- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex: (int)index{ 
UIImage *image = [imageArray objectAtIndex:index]; 
[openFlowView setImage:image forIndex:index]; 



笔者在OpenFlow库AFOpenFlowView.m文件的touchesEnded()函数中增加了双击回调接口,以便在用户双击图片时通知库的调用者,代码如下。 
//AFOpenFlowView.m 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
if(((UITouch *)[touches anyObject]).tapCount == 2){ 
if ([self.viewDelegate respondsToSelector:@selector(openFlowView: coverViewDoubleClick:)]) 
[self.viewDelegate openFlowView:self coverViewDoubleClick: selectedCoverView.number]; 

[super touchesEnded:touches withEvent:event]; 

库的调用者RootViewController类通过接口函数coverViewDoubleClick()即可处理用户双击事件,代码如下。 
- (void)openFlowView:(AFOpenFlowView *)openFlowView coverViewDoubleClick:(int)index{ 
NSLog(@"coverViewDoubleClick called!"); 
[self showPaintingViewController]; 


本节相关的完整Xcode工程源代码文件请参考本书附带的光盘中的OpenFlow工程。

 
 
 
 
分享到:1
 
 

 
级别: 小苹果
发帖
38
we券
47
贡献
0
经验
41
人气
0
 
只看该作者1楼 (沙发)  发表于: 2011-06-02 13:35
 
 
iPhone SDK开发基础之UIPageControl编程

当用户界面需要按页面进行显示时,使用iOS提供的UIPageControl控件将要显示的用户界面内容分页进行显示会使编程工作变得非常快捷,如图3-47所示就是一个使用UIPageControl控件逐页进行图片显示的程序,用户按下屏幕即可进行左右滚动显示,在屏幕的正上方使用白色的点显示当前滚动到的页面位置。

    
图3-47  UIPageControl编程实例界面
程序自定义一个SwipeView类,该类通过子类化UIView类并重载其touchesMoved()方法捕获用户滚动的方向,类的定义如下。
//  SwipeView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface SwipeView : UIView {
CGPoint startTouchPosition;
NSString *dirString;
UIViewController *host;
}
- (void) setHost: (UIViewController *) aHost;
@end

//  SwipeView.m
#import "SwipeView.h"
@implementation SwipeView
- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void) setHost: (UIViewController *) aHost
{
host = aHost;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = [touches anyObject]; 
startTouchPosition = [touch locationInView:self]; 
dirString = NULL;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
UITouch *touch = touches.anyObject; 
CGPoint currentTouchPosition = [touch locationInView:self]; 

#define HORIZ_SWIPE_DRAG_MIN 12 
#define VERT_SWIPE_DRAG_MAX 4 

if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= 
  HORIZ_SWIPE_DRAG_MIN && 
  fabsf(startTouchPosition.y - currentTouchPosition.y) <= 
  VERT_SWIPE_DRAG_MAX)  { 
  // Horizontal Swipe
  if (startTouchPosition.x < currentTouchPosition.x) {
   dirString = kCATransitionFromLeft;
  }
  else 
   dirString = kCATransitionFromRight;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (dirString) [host swipeTo:dirString];
}
@end

在捕获用户滚动的方向后,SwipeView类通过用户设置的host成员变量回调其swipeTo()方法,host成员变量在类中定义为UIViewController,在编译时编译器会产生警告,这里不用管它,只需要SwipeView类的使用者设置host成员变量并实现swipeTo()方法即可。
SwipeView类的使用者为PageViewController类,该类实现程序的主界面,在这个自定义的UIViewController类中实现swipeTo()方法,代码如下。
//  PageViewController.m
- (void) swipeTo: (NSString *) aDirection{
UIPageControl *pageControl = [[[contentView superview] subviews] lastObject];

if ([aDirection isEqualToString:kCATransitionFromRight])
{
  if (currentPage == 5) return;
  [pageControl setCurrentPage:currentPage + 1];
} else {
  if (currentPage == 0) return;
  [pageControl setCurrentPage:currentPage - 1];
}

[self pageTurn:pageControl];
}

在该回调方法中根据用户滚动的方向来设置UIPageControl的currentPage属性,如果是向右方滚动则页面计数加一,如果用户滚动的方向是向左,则页面计数减一。设置UIPageControl的currentPage属性以后,PageViewController对象再调用其pageTurn()方法交换页面显示内容,并将图片显示出来,代码如下。
- (void) pageTurn: (UIPageControl *) pageControl{
CATransition *transition;
int secondPage = [pageControl currentPage];
if ((secondPage - currentPage) > 0)
  transition = [self getAnimation:@"fromRight"];
else
  transition = [self getAnimation:@"fromLeft"];

UIImageView *newView = (UIImageView *)[[contentView subviews] objectAtIndex:0];
[newView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"ipad_ wallpaper%02d.jpg", secondPage + 1]]];
[contentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[[contentView layer] addAnimation:transition forKey:@"transitionView Animation"];

currentPage = [pageControl currentPage];
}


在主pageTurn()方法实现中,PageViewController类通过UIView的exchangeSubview AtIndex()方法实现页面内容的切换。
本节相关的完整Xcode工程源代码文件请参考本书附带的光盘中的PageControl工程。

本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。
 
 
 
 
 

 
级别: 红钻苹果
发帖
7442
we券
8512
贡献
0
经验
7392
人气
405
 
只看该作者2楼 发表于: 2011-06-03 12:43
 
 
好帖啊·····
 
 
 
 

 
级别: 小苹果
发帖
38
we券
47
贡献
0
经验
41
人气
0
 
只看该作者3楼 发表于: 2011-06-07 09:44
 
 
iPhone SDK开发基础之
使用UINavigationController组织和管理UIView
当你的程序具有层次化的工作流时,就比较适合使用UINavigationController来管理UIViewController,即用户可以从上一层界面进入下一层界面,在下一层界面处理完以后又可以简单地返回到上一层界面,UINavigationController使用堆栈的方式来管理UIViewController,进入下一层界面的代码如下。
[self.navigationController pushViewController:nextController animated:YES];
返回上一层界面的代码如下。
[self.navigationController popViewControllerAnimated:YES];
 
如图3-25所示,屏幕左上方的“Animal List”按钮是返回按钮,注意这个返回按钮是UINavigationController自动添加的,不需要编写任何代码在界面上添加按钮或者实现按钮操作,当程序使用pushViewController()函数将ViewController添加进UINavigation Controller的时候,UINavigationController就自动显示这个返回按钮,用户单击这个“Animal List”按钮就可以回到原先的界面,UINavigationController的这种运行机制产生这样的效果,用户可以一层一层地进入更深的界面层次,然后又可以一层一层的按顺序返回,使用这样的方式来组织用户界面非常方便。
本节相关的完整Xcode工程源代码文件请参考本书附带的光盘中的Zoo实例。

本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。

 
 
 
 
 

 
级别: 小苹果
发帖
38
we券
47
贡献
0
经验
41
人气
0
 
只看该作者4楼 发表于: 2011-06-07 09:47
 
 
iPhone SDK开发基础之
使用UITabBarController组织和管理UIView
当你的程序分为几个相对比较独立的部分时,就比较适合使用UITabBarController来组织用户界面,如图3-26所示。
 
图3-26  UITabBarController程序框架实例界面
在屏幕的下方包含UITabBarController的三个按钮,用户单击不同的按钮即可以进入不同的界面,每个界面相对来说在整个系统中比较独立,也就是程序分成了三个相对比较独立的不同部分,在每个相对独立的部分你也可以使用UINavigationController等容器类组织你的界面。这样组织使程序逻辑非常清晰,当然你也可以组织很多个Tab而不只是三个,以下代码演示如何创建UITabBarController对象,并为其添加多个Tab。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {    
    // Override point for customization after application launch.

//Create the navigation Controller
UINavigationController *localNavigationController;
//Create UINavigationController
tabBarController = [[UITabBarController alloc] init];
    tabBarController.delegate = self;
// Create the array that will contain all the View controlelr
NSMutableArray *localControllersArray = [[NSMutableArray alloc] init WithCapacity:3];
// Create the view controller attached to the first item in the TabBar

aViewController *firstViewController;
firstViewController = [aViewController alloc];
localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:firstViewController];
localNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;

[localNavigationController.tabBarItem initWithTitle:@"Outlines" 
image:[UIImage imageNamed:@"webcast.png"] tag:1];
firstViewController.navigationItem.title = @"Outlines";

[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[firstViewController release];

// Create the view controller attached to the second item in the TabBar

anotherViewController *secondViewController;
secondViewController = [[anotherViewController alloc] initWithStyle: UITableViewStyleGrouped ];
localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:secondViewController];
[localNavigationController.tabBarItem initWithTitle:@"Q & A" 
   image:[UIImage imageNamed:@"book.png"] tag:2];
secondViewController.navigationItem.title=@"Q & A";

[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[secondViewController release];
  
miscViewController *thirdViewController;
thirdViewController = [[miscViewController alloc] initWithStyle:UITable ViewStyleGrouped ];
localNavigationController = [[UINavigationController alloc] initWithRoot ViewController:thirdViewController];
[localNavigationController.tabBarItem initWithTitle:@"Misc" 
   image:[UIImage imageNamed:@"favorites.png"] tag:3];
thirdViewController.navigationItem.title=@"Misc";

[localControllersArray addObject:localNavigationController];
[localNavigationController release];
[thirdViewController release];

// load up our tab bar controller with the view controllers
tabBarController.viewControllers = localControllersArray;

// release the array because the tab bar controller now has it
[localControllersArray release];
// add the tabBarController as a subview in the window
[window addSubview:tabBarController.view];

// need this last line to display the window (and tab bar controller)
[window makeKeyAndVisible];

    return YES;
}
捕获Tab切换事件,获取当前活动的Tab索引和UIViewController对象,代码如下。
- (void)tabBarController:(UITabBarController *)barController didSelectView Controller:(UIViewController *)viewController{

NSLog(@"currentController index:%d",viewController, tabBarController.selectedIndex);
UIViewController  *currentController = tabBarController.selectedView Controller;
NSLog(@"currentController: %@",currentController);
   
}
切换不同的Tab时,只需要设置UITabBarController的selectedIndex属性即可,代码如下。
tabBarController.selectedIndex = 2;
本节相关的完整Xcode工程源代码文件请参考本书附带的光盘中的Lessons2实例。
本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。

 
 
 
 
 

 
级别: 小苹果
发帖
38
we券
47
贡献
0
经验
41
人气
0
 
只看该作者5楼 发表于: 2011-06-08 09:14
 
 
每天更新~~~~~~~~
 
 
 
 

 
级别: 小苹果
发帖
38
we券
47
贡献
0
经验
41
人气
0
 
只看该作者6楼 发表于: 2011-06-08 09:20
 
 
iPhone SDK开发基础之自定义仪表控件

在iOS开发中,因为程序的需要,有时要自行绘制iPhone SDK没有提供的界面控件,通常使用QuartzCore.framework即可画出你所需要的各种图形,在这里我们实现一个圆形的“仪表盘”控件,控件的外观如图3-48所示,用户可以通过旋转仪表控件的指针来设置程序需要的各种系统参数。
 
图3-48 “仪表盘”控件
控件使用两个UIView来实现仪表控件,并通过CGAffineTransform类来实现仪表指针的旋转,控件在UIDialView类中实现,UIDialView类的定义如下。
//  UIDialView.h
#import <UIKit/UIKit.h>

@protocol UIDialViewDelegate
@optional
- (void)dialValue:(int)tag Value:(float)value;
@end

@interface UIDialView : UIView {
id<UIDialViewDelegate> delegate;
NSTimer *timer;
UIImageView *iv;
float maxValue,minValue;
CGAffineTransform initialTransform ;
float currentValue;
}
@property(nonatomic,assign)id<UIDialViewDelegate>delegate;
@property CGAffineTransform initialTransform;
@property float currentValue;

@end
在UIDialView类的实现文件中,通过init()方法读取图片文件初始化控件背景和指针,代码如下。
//  UIDialView.m
#import "UIDialView.h"

@interface UIDialView()
-(void)spin:(NSTimer *)timer;
-(float) goodDegrees:(float)degrees;
@end

#define degreesToRadians(degrees) (M_PI * degrees / 180.0)
#define radiansToDegrees(radians) (radians * 180 / M_PI)

static CGPoint delta;
static float deltaAngle;
static float currentAngle;

@implementation UIDialView
@synthesize initialTransform,currentValue;

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

@synthesize delegate;
- (id)init{
    if ((self = [super init])) {
  
  self.userInteractionEnabled = YES;
  iv =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"knob. png"]];
  
  UIImage *backgroundTile = [UIImage imageNamed: @"clock.png"];
  UIColor *backgroundPattern = [[UIColor alloc] initWithPatternImage: backgroundTile];
  self.contentMode = UIViewContentModeCenter;
  [self setBackgroundColor:backgroundPattern];
  [backgroundPattern release];  
  
  iv.backgroundColor = [UIColor clearColor]; 
  iv.autoresizesSubviews= YES;  
  self.frame = CGRectMake(0, 0, iv.frame.size.width, iv.frame.size. height);
  
  [self addSubview:iv];  
  [self bringSubviewToFront:iv];
  [iv release];
  
  currentValue = 0;
  currentAngle = 0; 
  deltaAngle = 0.0;  
}
    return self;
}
在UIView的touchesBegan()方法中捕获用户Touch点的位置,并根据此位置使用atan2()函数计算出控件的初始化角度,代码如下。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *thisTouch = [touches anyObject];
delta = [thisTouch locationInView:self];

float dx = delta.x  - iv.center.x;
float dy = delta.y  - iv.center.y;
deltaAngle = atan2(dy,dx);
initialTransform = iv.transform;
}
在用户的旋转过程中通过设置指针UIView对象的transform属性实现仪表控件指针伴随用户手指的旋转而旋转,代码如下。
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:self];

float dx = pt.x  - iv.center.x;
float dy = pt.y  - iv.center.y;
float ang = atan2(dy,dx);
  
if (deltaAngle == 0.0) {
  deltaAngle = ang;
  initialTransform = iv.transform;  
}else{
  float angleDif = deltaAngle - ang;
  CGAffineTransform newTrans = CGAffineTransformRotate(initialTransform, -angleDif);
  iv.transform = newTrans;

  float diffValue = [self goodDegrees:radiansToDegrees(angleDif)];  
  currentValue = maxValue - ((maxValue - minValue)/300)*diffValue;
  if(currentValue > 100) currentValue = 100;

if (delegate != nil) {
  [delegate dialValue:self.tag Value:currentValue];
}
}
客户通过实现UIDialViewDelegate接口协议的dialValue()方法而得到控件的通知消息,代码如下。
//  DialViewController.h
#import <UIKit/UIKit.h>
#import "UIDialView.h"

@interface DialViewController : UIViewController< UIDialViewDelegate> {
   UIDialView *dialView;
   UILabel *myLabel;
}

- (void)dialValue:(int)tag Value:(float)value{
NSString *str = [NSString stringWithFormat:@"%.1f",v*100];
    [myLabel performSelector:@selector(setText:) withObject:str];
}
本节相关的完整Xcode工程源代码文件请参考本书附带的光盘中的DialControl工程。

本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。

 
 
 
 
 

 
级别: 小苹果
发帖
38
we券
47
贡献
0
经验
41
人气
0
 
只看该作者7楼 发表于: 2011-06-08 09:22
 
 
iPhone企业应用实例分析之二:
程序处理流程
程序处理流程总体框图如图5-4所示。
 
图5-4  程序处理流程图
(1)用户启动程序时,显示闪屏。
(2)显示系统主菜单,主要有“我的文档”、“部门文档”、“文档搜索”和“统计图查询”。
(3)用户选择“我的文档”以后显示需要我处理的文档列表。
(4)用户选择“部门文档”以后显示部门列表。
(5)用户选择“文档搜索”以后显示搜索条件设置界面。
(6)用户选择部门列表中的部门以后显示部门处理中的文档列表。
(7)用户设置搜索条件,单击搜索以后显示搜索结果文档列表。
(8)在文档列表界面显示文档名称、文档标识码,并可以前后翻页。
(9)用户选择文档列表中的文档时,显示该文档的详情,详情分为4个页面显示,第1页显示文档名称、作者、日期、状态等详细资料;第2页显示文档的附件,用户单击附件时可以将附件下载到手机;第3页显示文档处理历史记录;第4页显示文档工程流处理界面。
(10)用户选择“统计图查询”以后显示统计图列表。
(11)用户选择统计图列表中的记录时显示统计图。
(12)用户单击Info按钮时显示程序版本等信息。


本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书。
《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书已由电子工业出版社正式出版,本书由虞斌著。

 
 
 
 
 

 
级别: 红苹果
发帖
393
we券
405
贡献
0
经验
401
人气
13
 
只看该作者8楼 发表于: 2011-06-08 12:58
 
 
咱能大方点不?发个完整的给大家共享1
 
 
 
 

 
级别: 小苹果
发帖
38
we券
47
贡献
0
经验
41
人气
0
 
只看该作者9楼 发表于: 2011-06-09 09:08
 
 
iPhone企业应用实例分析之三: 
程序框架分析 
WebDoc Mobile项目是典型的多层流程型系统,所以系统主要使用UINavigation Controller进行用户界面的导航,这样当用户从上一层界面进入下一层界面,在下一层界面的事情处理完以后,又可以方便地返回到上一层界面,在用户登录系统以后,系统显示主菜单,如图5-6所示。 
主菜单分为4个选项,即“我的文档”、“部门文档”、“高级搜索”和“统计图”, 主菜单在MainViewController类中实现,该类使用UITableView来对菜单项进行管理, UITableView的数据源使用一个NSMutableArray来提供表格数据,表格绘制时,使用以下命令。 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{ 
 
图5-6  系统主菜单界面 
return menuList.count; 
}
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ 
   NSDictionary *dataDictionary = [menuList    objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [dataDictionary valueForKey:kTitleKey]; 
    return cell; 

返回表格行数和每行的具体内容,每行表格数据包含下一层ViewController界面的标题和类名称,当用户单击主菜单的菜单项时,程序先使用该类名称调用NSClassFromString()创建类对象,然后再创建ViewController对象,并将ViewController加入UINavigation Controller中进行界面显示。 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{ 
targetViewController = [[NSClassFromString(viewControllerName) alloc] initWithNibName:viewControllerName bundle:nil]; 
[self.navigationController pushViewController:targetViewController animated:YES]; 

在用户选择“我的文档”或者选择“部门文档”界面的具体部门后,以及使用“文档搜索”功能搜索文档后,程序就显示文档列表,如图5-7所示。 
 
图5-7  文档列表界面 
文档列表显示使用DocListViewController类实现,该类包含文档记录分页,前后导航功能,当记录超过每页记录显示的最大行数时,用户可以使用前向和后向箭头进行翻页,另外在用户进行搜索以后,程序也使用这个类来显示搜索结果。 
DocListViewController类包含一个UITableView成员变量,程序使用这个表格来显示文档列表,表格的数据源使用一个NSMutableArray来存储和提供数据,这个数组的每个元素都是一个DocumentDetailViewController类实例,在文档列表显示前使用一个遍历来创建这些DocumentDetailViewController类实例。 
for(i = 0; i < nResult; i++){ 
  DocumentDetailViewController *presidentsViewController = 
  [[NSClassFromString(viewControllerName) alloc] 
   initWithNibName:viewControllerName bundle:nil];   
  Document *doc = [objects objectAtIndex:i]; 
  presidentsViewController.title = doc.documentName; 
  [presidentsViewController setDocument:doc]; 
  [controllers addObject:presidentsViewController]; 
  [presidentsViewController release];       

当用户单击文档列表中的某个文档时,程序就从数组中取出对应的元素,然后显示DocumentDetailViewController对象。 
- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
[tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    NSUInteger row = [indexPath row]; 
if(self.controllers != nil){   
  DocumentDetailViewController *nextController = [self.controllers 
              objectAtIndex:row]; 
  //preload detail, attachment and history. 
  [nextController preLoadData]; 
     
  //clear cache 
  [self handleWithCache]; 

  //push document detail view... 
  [self.navigationController pushViewController:nextController animated:YES]; 

}
 
原文地址:https://www.cnblogs.com/DamonTang/p/2611420.html