IOS开发学习笔记(五)——多视图应用程序之框架分析
2013-06-01 19:12
686 查看
我们之前学习了Single View Application的创建和设置,当然我们的程序不可能只有一个界面,所以我们需要对多视图进行支持。
我们首先来看看最简单的多视图应用:Utility Application。我们新建一个工程,选择Utility类型,结果发现我们现在有2个ViewController:MainViewController和FlipsViewController。分别有.xib(xml格式的UI描述文件,类似于Android中的layout文件)、.h头文件、.m实现文件。
接下来我们通过分析源代码查看程序的执行逻辑:
对于MainView,我们看到xib右下角有个"info"的这种按钮,在执行时可以切换到FlipsView,我们查看源码得到的结果是:
对于FlipsView,我们查看“Done”按钮的事件处理源码:
接下来我们看一个Master-Detail的例子。Master-Detail类似于PC下的多窗口应用程序,主窗口中列表显示子窗口项,点击单项进入该项详情页面。
我们新建立一个工程之后分析:Master-Detail程序同样存在一个MasterView和一个DetailView。
其中MasterView中的代码:
Detail-View代码分析:
最后我们看一个Tabbed Application,这种类型的应用还是非常多的。我们以一个简单的例子来说明如果我们需要添加一个Tab页面的情况:
我们添加一个controller,我们选择New->iOS->Cocoa Touch中的Object-C Class,并且命名为:ThirdViewController,创建XIB文件。
我们修改AppDelegate.m代码,增加一个viewController:
ok,这时如果运行发现我们的TabView已经添加成功了,接下来我们需要修改View3的切换按钮和文字:
接下来可以对每个View编写自己的页面和逻辑。
我们首先来看看最简单的多视图应用: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开发学习笔记(六)——多视图应用程序之StoryBoard实践
- IOS开发基础教程学习笔记1(第6章)多视图应用程序
- 2011年冬斯坦福大学公开课 iOS应用开发教程学习笔记(第四课) Views 视图
- IOS学习笔记(十二)之IOS开发之表视图(UITableView)的相关类,属性与表视图实现学习(二)
- IOS学习笔记(十二)之IOS开发之表视图(UITableView)的讲解与使用(二)
- 2011年冬斯坦福大学公开课 iOS应用开发教程学习笔记(第四课) Views 视图
- IOS学习笔记(十一)之IOS开发之表视图(UITableView)的基本介绍(一)
- IOS学习笔记(十一)之IOS开发之表视图(UITableView)的基本介绍(一) 推荐
- IOS开发基础教程学习笔记3 使用Tab Bar切换视图
- 逐梦旅程学习笔记 DirectX开发入门01:应用程序基本框架
- iOS开发学习笔记 (一)表视图基础 Table Views
- iOS开发学习笔记③ - 视图控制器
- Silverlight商业应用程序开发--学习笔记(1) 导航框架 navigationFramework
- IOS开发学习笔记(七)——多线程应用程序开发
- objectc学习笔记----《马上着手开发 iOS 应用程序 (Start Developing iOS Apps Today)》
- iOS学习笔记-128.SDWebImage4——框架内部调用简单分析
- 斯坦福大学公开课 iOS应用开发教程学习笔记(第四课) Views 视图
- IOS学习笔记(一)之安装开发环境与工具与第一个IOS应用程序
- [iOS学习]iOS开发中视图相关的小笔记:push、modal、popover、replace、custom
- ios开发学习笔记040-autolayout 第三方框架Masonry