IOS开发学习笔记(五)——多视图应用程序之框架分析
来源:互联网 发布:网络大电影上帝玩笑 编辑:程序博客网 时间:2024/05/22 06:49
我们之前学习了Single View Application的创建和设置,当然我们的程序不可能只有一个界面,所以我们需要对多视图进行支持。
我们首先来看看最简单的多视图应用:Utility Application。我们新建一个工程,选择Utility类型,结果发现我们现在有2个ViewController:MainViewController和FlipsViewController。分别有.xib(xml格式的UI描述文件,类似于Android中的layout文件)、.h头文件、.m实现文件。
接下来我们通过分析源代码查看程序的执行逻辑:
- 对于MainView,我们看到xib右下角有个"info"的这种按钮,在执行时可以切换到FlipsView,我们查看源码得到的结果是:
// info按钮点击事件- (IBAction)showInfo:(id)sender{ // 获得FlipsViewController FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideViewController" bundle:nil]; // 设置Controller的delegate为MainView controller.delegate = self; // 设置切换动画 controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // 设置呈现Controller [self presentViewController:controller animated:YES completion:nil];}
- 对于FlipsView,我们查看“Done”按钮的事件处理源码:
// Done按钮事件处理- (IBAction)done:(id)sender{ [self.delegate flipsideViewControllerDidFinish:self];}
接下来我们看一个Master-Detail的例子。Master-Detail类似于PC下的多窗口应用程序,主窗口中列表显示子窗口项,点击单项进入该项详情页面。
我们新建立一个工程之后分析:Master-Detail程序同样存在一个MasterView和一个DetailView。
其中MasterView中的代码:
//// MasterViewController.m// MasterDetailDemo//// Created by Sam Wang on 13-5-27.// Copyright (c) 2013年 com.freesoft. All rights reserved.//#import "MasterViewController.h"#import "DetailViewController.h"@interface MasterViewController () { NSMutableArray *_objects;}@end@implementation MasterViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // 初始化标题 self.title = NSLocalizedString(@"Master", @"Master"); } return self;}- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. self.navigationItem.leftBarButtonItem = self.editButtonItem; // 初始化Add按钮样式 UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)]; self.navigationItem.rightBarButtonItem = addButton;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)insertNewObject:(id)sender{ // 插入新条目 if (!_objects) { _objects = [[NSMutableArray alloc] init]; } [_objects insertObject:[NSDate date] atIndex:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];}#pragma mark - Table View- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // 设置区域 return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // 条目数 return _objects.count;}// Customize the appearance of table view cells.- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 显示条目 static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } NSDate *object = _objects[indexPath.row]; cell.textLabel.text = [object description]; return cell;}- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ // Return NO if you do not want the specified item to be editable. return YES;}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete) { [_objects removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. }}/*// Override to support rearranging the table view.- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{}*//*// Override to support conditional rearranging of the table view.- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{ // Return NO if you do not want the item to be re-orderable. return YES;}*/- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // 选择某项条目 if (!self.detailViewController) { self.detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil]; } NSDate *object = _objects[indexPath.row]; self.detailViewController.detailItem = object; [self.navigationController pushViewController:self.detailViewController animated:YES];}@end
Detail-View代码分析:
//// DetailViewController.m// MasterDetailDemo//// Created by Sam Wang on 13-5-27.// Copyright (c) 2013年 com.freesoft. All rights reserved.//#import "DetailViewController.h"@interface DetailViewController ()- (void)configureView;@end@implementation DetailViewController#pragma mark - Managing the detail item- (void)setDetailItem:(id)newDetailItem{ if (_detailItem != newDetailItem) { _detailItem = newDetailItem; // Update the view. [self configureView]; }}- (void)configureView{ // Update the user interface for the detail item. // 显示条目内容 if (self.detailItem) { self.detailDescriptionLabel.text = [self.detailItem description]; }}- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. [self configureView];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ // 显示标题 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = NSLocalizedString(@"Detail", @"Detail"); } return self;}@end
最后我们看一个Tabbed Application,这种类型的应用还是非常多的。我们以一个简单的例子来说明如果我们需要添加一个Tab页面的情况:
- 我们添加一个controller,我们选择New->iOS->Cocoa Touch中的Object-C Class,并且命名为:ThirdViewController,创建XIB文件。
- 我们修改AppDelegate.m代码,增加一个viewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil]; UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; // 新增ViewController UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil]; self.tabBarController = [[UITabBarController alloc] init]; // 将ViewController添加到tabBar中 self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3]; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES;}
- ok,这时如果运行发现我们的TabView已经添加成功了,接下来我们需要修改View3的切换按钮和文字:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // 按钮文字 self.title = NSLocalizedString(@"Third", @"Third"); // 按钮图片 self.tabBarItem.image = [UIImage imageNamed:@"third"]; } return self;}
- 接下来可以对每个View编写自己的页面和逻辑。
- IOS开发学习笔记(五)——多视图应用程序之框架分析
- IOS开发学习笔记(六)——多视图应用程序之StoryBoard实践
- ios学习笔记之UI篇(六):多视图应用程序
- IOS开发基础教程学习笔记1(第6章)多视图应用程序
- MFC学习笔记——应用程序框架分析(CWinApp)
- iOS开发学习笔记——分段视图(UISegmentedControl)
- Android 入门开发指南之五—分析应用程序
- cocos2dx游戏开发学习笔记(五)之引擎框架
- ios学习笔记之UI篇(五):切换视图
- iPhone开发基础教程笔记(五)--第六章 多视图应用程序
- iOS开发之高级视图—— UITableView(五)表视图索引
- IOS学习之旅之7 多视图应用程序
- GTK+图形化应用程序开发学习笔记(十七)—框架构件、比例框架构件
- GTK+图形化应用程序开发学习笔记(十七)—框架构件、比例框架构件
- iOS开发笔记之六——视图的生命周期
- IOS5基础之五-----多视图应用程序
- 【Facebook的UI开发框架React入门之五】视图View与文本Text简介(iOS平台)-goodmao
- IOS开发学习笔记(五)
- 亚马逊将于6月13日发售升级版平板电脑而手机依旧了无音讯
- 网络流量计费程序(1)
- 初识面向对象设计(一)
- DC、CDC及CDC的各个子类
- .apk逆向工程
- IOS开发学习笔记(五)——多视图应用程序之框架分析
- 在线编译器,非常方便于:学习、测试
- MPLS 标记分配方法
- centos5.5下安装samba服务器以及windows访问samba服务器
- 响应式web css框架集合
- [跟我学ROS]9.使用rosed编辑文件
- 正则表达式使用详解
- BZOJ 3209(花神的数论题-数位统计+1,被数据范围坑了)
- Vim简明教程