在 .h 文件中
#import "FolderViewController.h"
#import "FileViewController.h"
@interface mainViewController : UIViewController {
FolderViewController *folderViewObj;
FileViewController *fileViewObj;
}
在 .m 文件中
- (void)viewDidLoad {
[super viewDidLoad];
folderViewObj = [[FolderViewController alloc] initWithNibName:@"FolderViewController" bundle:nil];
fileViewObj = [[FileViewController alloc] initWithNibName:@"FileViewController" bundle:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tblObj cellForRowAtIndexPath:indexPath];
NSString *lblText = cell.DetailTextLabel.text;
if ([lblText isEqualToString:@"Folder"]) {
[self.navigationController pushViewController:folderViewObj animated:YES];
}
else if ([lblText isEqualToString:@"File"])
{
[self.navigationController pushViewController:fileViewObj animated:YES];
}
}
-(void) dealloc {
[folderViewObj release];
[fileViewObj release];
[super dealloc];
}
使用这种方式,当用户可以选择 uitableview 的行时,FolderViewController 和 FileViewController 的对象只创建一次,而不是所有时间。