Modal View Controller Example[转]

简介:

In your iPhone app, you’ll probably be spending most of the time pushing new view controllers to the stack in order to show screen flow. Sometimes, though, you just want to popup a screen for quick display or input.

Here’s a quick demo/tutorial on the different standard modal views offered by iOS, including a simple way of passing generic string data back to the parent view. I’m assuming basic knowledge of iPhone programming, so feel free to skim if you’re comfortable.

First up, create a new Xcode View-Based application. I named mine “ModalViewExample”. Open up the NIB file “ModalViewExampleViewController.xib” and drag four buttons onto the screen as shown.


Now, we’ll begin attaching the buttons created in Interface Builder to our View Controller code. Open “ModalViewExampleViewController.h” and declare your buttons as IBOutlets (Interface Builder Outlets):

1

2

3

4

5

6

7

8

9

// ModalViewExampleViewController.h

 

@interface ModalViewExampleViewController : UIViewController  {

    UIButton *showDefaultButton, *showFlipButton, *showDissolveButton, *showCurlButton;

}

 

@property (nonatomic, retain) IBOutlet UIButton *showDefaultButton, *showFlipButton, *showDissolveButton, *showCurlButton;

 

@end

Make sure you synthesize the outlets in ModalViewExampleViewController.m, and release them in dealloc

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// ModalViewExampleViewController.m

 

#import "ModalViewExampleViewController.h"

 

@implementation ModalViewExampleViewController

 

@synthesize showDefaultButton, showFlipButton, showDissolveButton, showCurlButton;

 

[...]

 

- (void)dealloc {

    [showDefaultButton release];

    [showFlipButton release];

    [showDissolveButton release];

    [showCurlButton release];

    [super dealloc];

}

 

@end

Ok, so now we have some buttons declared in our class, and some buttons dropped into the interface. At this point, your app has no idea what the relationship is. We’ll set these relationships in Interface Builder.

Switch back to your NIB file, and select “File’s Owner” in the NIB Object Window. Select the Connections Inspector tab, and drag a connection from the hollow point to the Button object in your View. You can see what I mean in the screenshot below. Repeat for the other buttons on the page.


Basically, what we’ve told Xcode to do is relate the code-based objects with the interface objects we created. This allows them to pass messages back and forth.

The next step is to actually assign methods to the click events. Go back to your ModalViewExampleViewController.h file and add these methods:

1

2

3

4

- (IBAction)showDefault:(id)sender;

- (IBAction)showFlip:(id)sender;

- (IBAction)showDissolve:(id)sender;

- (IBAction)showCurl:(id)sender;

You’ll also need to implement them in ModalViewExampleViewController.m:

1

2

3

4

5

6

7

8

9

10

11

- (IBAction)showDefault:(id)sender {

}

 

- (IBAction)showFlip:(id)sender {

}

 

- (IBAction)showDissolve:(id)sender {

}

 

- (IBAction)showCurl:(id)sender {

}

We now have definitions for buttons, and the actions to go with them. To connect them, go to Interface Builder. Select one of your buttons and drag a relationship between the “Touch Up Inside” event in the Connections Inspector and the First Responder in the Object Window. Select the name of the method you created.


After connecting all the buttons to their relative objects and actions, you’ll actually need to make the app do something. This is where we will define a modal view to present to the user.

Add a new UIViewController subclass with XIB to your project. I named mine “SampleViewController”. Open the XIB in Interface Builder and drag a new button onto the screen.


Once you’ve done that, create an IBOutlet for the button and define an action as above.

1

2

3

4

5

6

7

8

9

10

11

// SampleViewController.h

 

@interface SampleViewController : UIViewController {

    UIButton *dismissViewButton;

}

 

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

 

- (IBAction)dismissView:(id)sender;

 

@end

Be sure to go to Interface Builder and assign the connections as you did above. Once that’s done, you’ll need to synthesize and release the dismissViewButton object in SampleViewController.m as you did in ModalExampleViewController.m.

Assuming all your connections are in place, it’s time to actually do something with them. Switch to your ModalViewExampleViewController class and replace your previous method definitions with the following code. Also be sure to add #import “SampleViewController.h” to the top of your definition in order to expose SampleView to your class.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

- (IBAction)showDefault:(id)sender {

    SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];

    [self presentModalViewController:sampleView animated:YES];

}

 

- (IBAction)showFlip:(id)sender {

    SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];

    [sampleView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

    [self presentModalViewController:sampleView animated:YES];

}

 

- (IBAction)showDissolve:(id)sender {

    SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];

    [sampleView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

    [self presentModalViewController:sampleView animated:YES];

}

 

- (IBAction)showCurl:(id)sender {

    SampleViewController *sampleView = [[[SampleViewController alloc] init] autorelease];

    [sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];

    [self presentModalViewController:sampleView animated:YES];

}

Let’s take a very quick look at what we’re doing. First, we define an instance of the new SampleViewController class. We then give it a modal transition style, based on the values found in the UIViewController documentation. Sending it to the front is as easy as using presentModalViewController:animated: Take note that you should probably use the navigation controller to push modal views if you have one. (i.e. [self.navigationController presentModalViewController:animated:]).

To finish up, we’ll need to code a way to dismiss the modal view once it’s presented to the user. Jump to the SampleViewController class and add this code:

1

2

3

- (IBAction)dismissView:(id)sender {

    [self dismissModalViewControllerAnimated:YES];

}

Once that’s in place, build and run the project. You should be able to click through the buttons and check out the range of different modal views iOS has to offer. Note that the page curl only works in iOS 3.2 or greater. If you’re compiling for lower versions, you’ll just get the default slide up animation.


欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 330987132 | Go:217696290 | Python:336880185 | 做人要厚道,转载请注明出处!http://www.cnblogs.com/sunshine-anycall/archive/2011/11/22/2259316.html
相关文章
|
传感器 算法 物联网
《移动互联网技术》第三章 无线定位技术:掌握位置服务和室内定位的基本概念和工作原理
《移动互联网技术》第三章 无线定位技术:掌握位置服务和室内定位的基本概念和工作原理
345 0
|
6月前
|
SQL 弹性计算 运维
Hologres计算组实例&分时弹性入门实践
本文由骆撷冬(Hologres PD)撰写,围绕Hologres计算组实例与分时弹性的入门实践展开。内容分为三部分:第一部分介绍Hologres计算组实例的原理与架构,解决负载隔离、资源浪费、大任务和运维难题;第二部分演示计算组实例的入门实践,包括管理、授权、连接及监控等操作;第三部分讲解分时弹性的使用,涵盖配置方法、成本优化及监控告警。通过具体案例与操作步骤,帮助用户更好地理解和应用Hologres的弹性计算能力。
|
8月前
|
存储 算法 C语言
【C语言程序设计——函数】素数判定(头歌实践教学平台习题)【合集】
本内容介绍了编写一个判断素数的子函数的任务,涵盖循环控制与跳转语句、算术运算符(%)、以及素数的概念。任务要求在主函数中输入整数并输出是否为素数的信息。相关知识包括 `for` 和 `while` 循环、`break` 和 `continue` 语句、取余运算符 `%` 的使用及素数定义、分布规律和应用场景。编程要求根据提示补充代码,测试说明提供了输入输出示例,最后给出通关代码和测试结果。 任务核心:编写判断素数的子函数并在主函数中调用,涉及循环结构和条件判断。
370 23
|
6月前
|
人工智能 IDE 测试技术
魔搭×通义灵码:0代码基础、0门槛在线编程做应用
本节课主要介绍了如何利用 Notebook IDE 环境和通义灵码工具来具体开发 AI 产品,通过前面的介绍,可以感受到好的开发环境和开发工具往往可以让开发过程事半功倍,也可以更快更好地解决一些实际问题。随着 AI 代码生成工具不断成熟,动动手指,你的 AI 产品马上变成现实~
|
10月前
|
存储 安全 数据安全/隐私保护
Docker的安全性体现在哪些方面?
综上所述,Docker 通过一系列的安全机制和措施,为用户提供了相对可靠的安全保障。然而,如同任何技术一样,Docker 的安全也需要持续的关注和维护,用户应保持警惕,不断加强安全管理和防范措施,以应对不断变化的安全威胁。在利用 Docker 带来便利的同时,也要确保其安全性,为应用的稳定运行和数据的安全提供坚实的保障。
415 60
|
存储 安全 数据安全/隐私保护
在Docker中,Docker安全么?
在Docker中,Docker安全么?
|
8月前
|
数据采集 人工智能 自然语言处理
魔搭社区每周速递(1.5-1.18)
🙋魔搭ModelScope本期社区进展:新增3239个模型,711个数据集,192个创新应用, 16篇内容
448 11
|
11月前
|
编译器 C语言
C语言:typedef 和 define 有什么区别
在C语言中,`typedef`和`#define`都是用来创建标识符以简化复杂数据类型或常量的使用,但它们之间存在本质的区别。`typedef`用于定义新的数据类型别名,它保留了数据类型的特性但不分配内存。而`#define`是预处理器指令,用于定义宏替换,既可用于定义常量,也可用于简单的文本替换,但在编译前进行,过度使用可能导致代码可读性下降。正确选择使用`typedef`或`#define`可以提高代码质量和可维护性。
|
弹性计算 运维 自然语言处理
启迪操作系统智慧的神:操作系统智能助手OS Copilot
OS Copilot 是阿里云针对Linux推出的一款智能助手,基于大模型构建,简化了Linux操作和运维工作。它支持自然语言问答,辅助命令执行,阿里云CLI调用以及系统运维和调优。这款工具特别适合初学者和运维人员,减少了对命令记忆的需求,通过对话式交互即可完成任务。
419 15
|
存储 弹性计算 数据库
云计算概念和与云服务的区别
“云”在计算机科学和信息技术领域通常指“云计算”,即通过互联网提供计算资源(如服务器、存储、数据库、网络、软件、分析等)的模式。用户可以按需访问和使用这些资源,而无需管理和维护实际的硬件和软件。
1601 3