iPhone企业应用实例分析之四:技术要点分析(3)
1.目录浏览器制作
在WebDoc Mobile项目中,用户需要将手机本地的文件上传到服务器端,iOS并没有提供目录浏览控件供开发者使用,所以只有自行开发实现目录浏览的功能,如图5-18所示是实现的界面。

▲图5-18 目录浏览控件用户界面
程序递归根目录下的所有目录和文件,目录以 图标显示,文件以 图标显示,当用户单击目录图标时,程序列出该目录下的所有文件和目录,用户单击返回按钮又可以返回上一层目录,当用户单击文件图标时,程序显示文件的详情,如修改日期、文件大小等明细信息,如图5-19所示。
手机目录浏览器的实现,在DirectoryViewController类中,该类继承自UITableView Controller,并实现UINavigationControllerDelegate接口,以便使用UINavigationController的导航功能,从文件目录的下一层方便地返回到目录的上一层,在DirectoryViewController类中使用一个NSArray类型的成员变量directoryContents来存储文件和目录名,并作为表格控件的数据源,在创建DirectoryViewController对象以后,程序使用NSFileManager类的directoryContentsAtPath方法列出指定目录下所有的文件和目录,并赋值给directory Contents成员变量。

▲图5-19 文件详情用户界面
[directoryContents release];
directoryContents = [[NSMutableArray alloc] init];
directoryContents = [[NSFileManager defaultManager]
directoryContentsAtPath: directoryPath];
[directoryContents retain];
}
在表格绘制时,查询directoryContents成员变量的元素个数就得到表格的行数,查询表格行对应的数组元素就得到文件或者目录的名称,这样就可以正确显示每一行的内容。
…
2.文件上传和下载
5.7.7节通过UITableView和UINavigationController两个类实现目录浏览器,在用户浏览到想要上传的文件后,选择该文件就可以将其上传到服务器,在文件的上传过程中,需要显示上传进度,以便用户了解当前的进度,如图5-20所示。
下面来看具体如何使用UIAlertView实现文件上传时的进度条显示功能。

▲图5-20 文件上传用户界面
withActivity:(BOOL)activity{
if(progressAlert != nil){
[progressAlert release];
progressAlert = nil;
}
// This timer takes the place of a real task
amt = 0.0;
timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
target: self
selector: @selector (handleTimer:)
userInfo: nil
repeats: YES];
if(!progressAlert)
{
progressAlert = [[UIAlertView alloc] initWithTitle: message
message: @"Please wait..."
delegate: self
cancelButtonTitle: nil
otherButtonTitles: nil];
// Create the progress bar and add it to the alert
if (activity) {
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicator ViewStyleWhite];
activityView.frame = CGRectMake(139.0f-18.0f, 80.0f, 37.0f, 37.0f);
[progressAlert addSubview:activityView];
[activityView startAnimating];
[activityView release];
} else {
progressView = [[UIProgressView alloc] initWithFrame:CGRectMake (30.0f, 80.0f, 225.0f, 90.0f)];
[progressAlert addSubview:progressView];
[progressView setProgressViewStyle: UIProgressViewStyleBar];
[progressView release];
}
// Add a label to display download/upload size.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(90.0f, 90.0f, 225.0f, 40.0f)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.font = [UIFont systemFontOfSize:12.0f];
label.text = @"";
label.tag = 1;
[progressAlert addSubview:label];
}
[progressAlert show];
}
程序使用一个NSTimer来处理上传超时,若超时则终止上传操作。
{
amt += 1;
if(progressView != nil)
[progressView setProgress: (amt / DOWNLOAD_TIMEOUT)];
if (amt > DOWNLOAD_TIMEOUT) {
UILabel *label = (UILabel *)[progressAlert viewWithTag:1];
label.text = @"Sorry, Time Out...";
[atimer invalidate];
atimer = nil;
[progressAlert dismissWithClickedButtonIndex:0 animated:TRUE];
[progressAlert release];
progressAlert = nil;
}
}
函数调用:
文档附件上传界面如图5-21所示,用户单击“AttachFile”按钮,程序显示目录列表供用户浏览和选择文件,用户选择具体的文件后即可将文件上传至服务器。

▲图5-21 文档附件上传界面
本文节选自《iOS软件开发揭密:iPhone&iPad企业应用和游戏开发》一书,由电子工业出版社正式出版,本书由虞斌著。

▲图书