http://useyourloaf.com/blog/2010/5/3/ipad-modal-view-controllers.html

非常好的一篇文章。

There were some minor tweaks to modal view controllers with iPhone OS 3.2 that are worth taking a look at if you need a modal view for an iPad or universal app.

Modal View Controller Recap

Modal view controllers are well documented in the View Controller Programming Guide for iPhone OS but here is a quick recap of how to present and dismiss a modal view. To keep things simple I will cover the steps for presenting the modal view when a button is pressed on a master view controller. When the button is pressed we need to create and show a detail view controller that contains a button that the user can use to dismiss the modal view controller. The NIB file for the detail view controller is trivial in this example consisting of just a single view containing a text label.

Allocating and showing the detail view controller is straightforward:

- (void)buttonAction:(id)sender {

// Create the modal view controller

DetailViewController *viewController = [[DetailViewController alloc]

initWithNibName:@”DetailViewController” bundle:nil];

// We are the delegate responsible for dismissing the modal view

viewController.delegate = self;

// Create a Navigation controller

UINavigationController *navController = [[UINavigationController alloc]

initWithRootViewController:viewController];

// show the navigation controller modally

[self presentModalViewController:navController animated:YES];

// Clean up resources

[navController release];

[viewController release];

}

After allocating and initializing the controller from its NIB file the delegate (which I will explain shortly) is set to the current master view controller. The important step is that we do not display the detail view controller directly. We actually create a navigation bar controller and initialize it with the detail view controller. This provides a convenient navigation bar to hold a button for dismissing the view. Once the navigation bar is configured correctly it is displayed with thepresentModalViewController method of UIViewController.

The detail view controller needs to add a “Done” button to the naviagtion bar which will trigger the master view controller to dismiss the modal view. The viewDidLoad method is the best place to add the button:

- (void)viewDidLoad {

[super viewDidLoad];

// Override the right button to show a Done button

// which is used to dismiss the modal view

self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemDone

target:self

action:@selector(dismissView:)] autorelease];

}

The target action of the button is the method dismissView in the detail view controller. Best practise is that the view controller that created the modal view should also dismiss it. We could do this by adding an instance variable to the detail view controller of type MasterViewController and initialize it to contain a reference to the master view controller when we first create the modal view. However this makes the DetailViewController directly dependent on the master view controller which is a pain if we later want to use this modal view from elsewhere in the application.

A better solution is to make use of the delegate protocol pattern. We define a protocol in the detailViewController that all callers of the modal view must implement. The protocol ensures that all delegates of our class must implement the didDismissModalView method:

@protocol ModalViewControllerDelegate <NSObject>

- (void)didDismissModalView;

@end

The interface for the DetailViewController contains a weak reference to the delegate as follows:

@interface DetailViewController : UIViewController {

id<ModalViewControllerDelegate> delegate;

}

@property (nonatomic, assign) id<ModalViewControllerDelegate> delegate;

You should also remember to @synthesize delegate in the implementation of DetailViewController. We can now implement the dismissView method in the DetailViewController:

// Done button clicked

- (void)dismissView:(id)sender {

// Call the delegate to dismiss the modal view

[delegate didDismissModalView];

}

This illustrates the advantage of delegation. The DetailViewController does not now need to know who the calling controller is it only has to call the didDissmisModalView method. The compiler ensures that this method exists in the calling controller. The MasterViewController must declare that it implements the delegation protocol:

#import “DetailViewController.h”

@interface MasterViewController : UIViewController <ModalViewControllerDelegate> {

UIButton *button;

}

@property (nonatomic,retain) IBOutlet UIButton *button;

- (IBAction)buttonAction:(id)sender;

@end

In this simple example the implementation of the didDismissModalViewmethod is trivial:

- (void)didDismissModalView {

// Dismiss the modal view controller

[self dismissModalViewControllerAnimated:YES];

}

In more complex situations this method might validate changes to an object made by the user in the modal view prior to updating the model. The Apple View Controller Programming Guide nicely sums up the advantages of this approach:

  • The delegate object has the opportunity to validate or incorporate changes from the modal view controller before that view controller is dismissed.
  • The use of a delegate promotes better encapsulation because the modal view controller does not have to know anything about the parent object that presented it. This enables you to reuse that modal view controller in other parts of your application.

Modal Presentation Styles

Prior to iPhone OS 3.2 the modal view filled the whole screen which on an iPhone sized device is probably the only option. However with the larger screen size of an iPad there are some more options to control the appearance of the modal view. There is a new view controller property named modalPresentationStyle which allows you to select the required style when creating the view controller:

  • UIModalPresentationFullScreen: This is the default style and as the name suggests causes the modal view to be presented full screen exactly as with the iPhone
  • UIModalPresentationPageSheet: The modal view occupies the full screen height but the width is set to the width of the screen in portrait mode. This means that when the device is in portrait mode it still occupies the full screen. However in landscape mode it leaves some of the underlying master view to still be visible, but dimmed as the user cannot interact with the uncovered master view controller.
  • UIModalPresentationFormSheet: Both the width and height of the modal view are set to be smaller than the screen size. The modal view is centered on the screen and the uncovered areas of the master view controller are dimmed to prevent user interaction. If the modal view makes used of the keyboard the modal view is moved upwards to make room.
  • UIModalPresentationCurrentContext: The modal view is displayed using the same style as the parent master view controller.

The differences between the Page and Form sheet styles in landscape mode are easier to see than explain:

               

Using these new styles can make for a better user experience when what you have to display cannot usefully take advantage of the larger full screen space on the iPad. Showing the detailed content in a smaller than full screen window with the rest of the interface dimmed can help the user focus on the modal view. Note that on an iPhone or iPod Touch the modal view controller is always displayed full screen. To use one of the new presentation styles set the property on the controller you are presenting modally. In our previous example this would be the navigation controller in the buttonAction method of the master view controller:

navController.modalPresentationStyle = UIModalPresentationFormSheet;

There are some limitations on when you can use these new styles. If you are using the UIModalTransitionStylePartialCurl style for example you must use the full screen presentation style or you will get an exception.

Modal Views in Popovers

One final change introduced in iPhone OS 3.2 that impacts modal view controllers is their use with popovers. A popover is generally expected to be dismissed when the user touches outside of the popover. Of course if you are displaying a modal view controller in the popover this behaviour is probably not what you want. To prevent this you can set the modalInPopoverboolean for the view controller. With the flag set the popover will not be dismissed by touches outside of the popover.

转载于:https://www.cnblogs.com/sesexxoo/archive/2012/03/08/6190001.html

Modal View Controllers, Not Model-View-Controller(MVC)相关推荐

  1. 关于Presenting view controllers on detached view ...

    为什么80%的码农都做不了架构师?>>>    使用模态跳转,Xcode有时候会出现 Presenting view controllers on detached view con ...

  2. 更轻量的 View Controllers

    原文链接:http://objccn.io/issue-1-1/ View controllers 通常是 iOS 项目中最大的文件,并且它们包含了许多不必要的代码.所以 View controlle ...

  3. Hello Qt(三十三)——Model/View官方文档

    一.Model/View框架简介 Qt4推出了一组新的项视图类,使用Model/View框架来管理数据与表示层的关系.Model/View框架带来的功能上的分离给了开发人员更大的弹性来定制数据项的表示 ...

  4. 第15.22节 PyQt(Python+Qt)入门学习:Model/View架构详解

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.简介 在PyQt和Qt中,Model/View架构是图形界面开发时用于管理数据和界面展现方式的关 ...

  5. Model/View 教程

     说明:这篇博客基本都是翻译于Qt官方的Model/View Tutorial教程,无法理解的地方建议转到原文,同时,由于译者水平有限,如有差错欢迎指出. 原文:http://qt-project ...

  6. Qt Model/View教程

    修正版已转移到 Qt中文文档 目录 修正版已转移到 [Qt中文文档](https://www.qtdoc.cn/Src/M/Model_View_Tutorial/Model_View_Tutoria ...

  7. MVC(Model View Controller)框架

    MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...

  8. ASP.NET MVC one view bind many model

    一.自定义视图模型 model.cs public class AorBvm{public List<Role> GetRole { get; set; }public List<C ...

  9. MVC的全名是Model View Controll

    MVC的全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,是一种软件设计典范.它是用一种业务逻辑.数据与界面显示分离的方法 ...

最新文章

  1. 使用 P3P 规范让 IE 跨域接受第三方 cookie
  2. linux 内核配置 dns,Linux的dns配置 - Linux操作系统基础进阶练习题_Linux教程_Linux公社-Linux系统门户网站...
  3. 160个Crackme033
  4. 5分钟搞定Loki告警多渠道接入
  5. android slidingdrawer 方向,如何使Android SlidingDrawer从左侧滑出?
  6. android 内存抖动_android内存泄漏怎么破?一招教你搞定!
  7. c++多线程——线程启动
  8. 在linux系统下安装oracle前的准备工作(配置oracle环境变量)
  9. DeepStream运行范例出错,提示缺少libnvinfer.so怎么办?
  10. python 比较两文件夹的内容,具有通用性。
  11. JDK动态代理(通俗白话)
  12. C语言全局变量和局部变量同名时的使用
  13. 上海复旦大学校友会曾鸣: 互联网的本质
  14. Total Variation
  15. php搭建markdown云笔记_Typora+坚果云:支持markdown的云笔记搭建
  16. Android带标题的列表,Listview Section 多个标题以及内容
  17. winbox基础应用教程
  18. 网络经济与企业管理【六】之企业运作管理
  19. .NET破解之PDFdo转换器
  20. GA-Z77-D3H主板装linux系统出现网卡找不到的问题

热门文章

  1. sas数字转日期格式_[转载]SAS日期格式输出格式大全
  2. mysql查询结果做表_MySQL将一个表的查询结果作为本表的查询条件更新数据
  3. maven伺服搭建_nexus搭建maven私服
  4. 学计算机辐射,离散数学对计算机专业系统知识辐射作用.doc
  5. Hybrid Skeleton Driven Surface Registration for Temporally Consistent Volumetric Video
  6. papers to read
  7. Farthest points Sampling on 3D meshes with mesh kept based on diffusion distance
  8. Multidimensional Scaling (MDS)
  9. DX中关于error C2664: “ID3DXEffect::SetMatrix”: 不能将参数 1 从“const char [17]”转换为“D3DXHANDLE”
  10. PaddleOCR 文本检测训练+推理模型转换教程