Modal View Controller Example – Part 2[转]

简介:

n the first part of this tutorial, we set up a pair of simple views in Interface Builder that we switched between modally. In this tutorial, we’ll make them somewhat useful and pass data between them using delegates.

The concept of protocols and delegates is an important and somewhat complex one, but I like to think of it in these simplified terms:


Basically, the object that implements our protocol agrees to implement the methods of that protocol. In the case of this tutorial, we’ll be connecting the modal view with our main view using a delegate.

Firstly, we’ll create the interface elements for our project. Open the ModalViewExampleViewController XIB file and create a button and a label as shown.


Next, add those interface elements to ModalViewExampleViewController.h. We’re also adding the necessary IBAction also:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

//  ModalViewExampleViewController.h

 

@interface ModalViewExampleViewController : UIViewController {

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

    UIButton *showWithDelegateButton;

    UILabel *myMessage;

}

 

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

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

@property (nonatomic, retain) IBOutlet UILabel *myMessage;

 

- (IBAction)showDefault:(id)sender;

- (IBAction)showFlip:(id)sender;

- (IBAction)showDissolve:(id)sender;

- (IBAction)showCurl:(id)sender;

- (IBAction)showWithDelegate:(id)sender;

 

@end

Be sure to include the necessary additions to ModalViewExampleViewController.m:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

//  ModalViewExampleViewController.m

#import "ModalViewExampleViewController.h"

#import "SampleViewController.h"

 

@implementation ModalViewExampleViewController

 

@synthesize showDefaultButton, showFlipButton, showDissolveButton, showCurlButton;

@synthesize showWithDelegateButton, myMessage;

...

- (IBAction)showWithDelegate:(id)sender {

 

}

...

- (void)dealloc {

    [showDefaultButton release];

    [showFlipButton release];

    [showDissolveButton release];

    [showCurlButton release];

    [showWithDelegateButton release];

    [myMessage release];

    [super dealloc];

}

 

@end

Jump to Interface Builder and be sure to link the new elements with the properties we defined. Refer to Part 1 of this tutorial for a guide on how to do that.


The next step is key. We will create a basic protocol and then assign a delegate. Open up ModalViewExampleViewController.h and add this:

1

2

3

4

5

6

// ModalViewExampleViewController.h

@protocol ModalViewDelegate

 

- (void)didReceiveMessage:(NSString *)message;

 

@end

We then tell ModalViewExampleViewController to implement this protocol:

// ModalViewExampleViewController.h

@interface ModalViewExampleViewController : UIViewController <ModalViewDelegate>

We also need to add the protocol’s method to the main implementation:

1

2

3

4

// ModalViewExampleViewController.m

- (void)didReceiveMessage:(NSString *)message {

 

}

Once we have these in place, the next step is to set up a reference between the two views. What we will do is define a delegate inside of SampleView so that we can send messages to it.
Include the protocol in ModalViewExampleViewController.h and add the reference:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

// SampleViewController.h

@protocol ModalViewDelegate;

 

@interface SampleViewController : UIViewController {

    id<ModalViewDelegate> delegate;

 

    UIButton *dismissViewButton;

}

 

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

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

 

- (IBAction)dismissView:(id)sender;

 

@end

Be sure to synthesize the delegate in SampleViewController.m, and include the necessary header files.

1

2

3

4

5

6

7

8

#import "SampleViewController.h"

#import "ModalViewExampleViewController.h"

 

@implementation SampleViewController

 

@synthesize dismissViewButton;

@synthesize delegate;

...

So far we have defined a protocol inside of our parent view, and defined a delegate in our modal view. The next step is to link them together and make them useful.
Firstly, we’ll write the functions that will handle the messages. Replace the original definition of didReceiveMessage with this:

1

2

3

- (void)didReceiveMessage:(NSString *)message {

[myMessage setText:message];

}

And also add the following code to showWithDelegate:

1

2

3

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

 sampleView.delegate = self;

 [self presentModalViewController:sampleView animated:YES];

What we’ve done is create a SampleView object and assigned its delegate to be the parent view. That is, the parent view will be handling messages sent by SampleView.
Open up SampleViewController.m and add the code to send the message.

1

2

3

4

5

6

// SampleViewController.m

- (IBAction)dismissView:(id)sender {

    [delegate didReceiveMessage:@"Hello World"];

 

    [self dismissModalViewControllerAnimated:YES];

}

Compile the app and run it. You should be able to see the text “Hello World” passed from one view to another once you dismiss your modal view with delegate. You can extend this any way you like with additional controls on the modal view, such as sliders or text input.

Download the source code for this project here.

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!












本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2011/11/22/2259441.html ,如需转载请自行联系原作者


相关文章
|
12月前
|
机器学习/深度学习 存储 监控
揭秘微调‘失忆’之谜:如何运用低秩适应与多任务学习等策略,快速破解灾难性遗忘难题?
【10月更文挑战第13天】本文介绍了几种有效解决微调灾难性遗忘问题的方法,包括低秩适应(LoRA)、持续学习和增量学习策略、记忆增强方法、多任务学习框架、正则化技术和适时停止训练。通过示例代码和具体策略,帮助读者优化微调过程,提高模型的稳定性和效能。
454 5
|
监控 Shell API
了解asyncio高级api索引
【6月更文挑战第27天】本文是`asyncio` 高级API概览:运行异步任务如`run()`, `create_task()`;等待机制如`gather()`, `wait_for()`, `shield()`;任务管理如`current_task()`, `all_tasks()`;队列和子进程功能;同步原语包括锁、事件和信号量。示例中涉及`sleep()`, `gather()`, `wait_for()`, 子进程创建及同步异常`TimeoutError`和`CancelledError`。查阅官方文档以获取详细信息和示例代码。
224 1
了解asyncio高级api索引
|
人工智能
ACL 2024 Oral:大模型也会被忽悠?揭秘AI的信念之旅
【8月更文挑战第28天】清华大学、上海交通大学、斯坦福大学和南洋理工大学的研究团队最新研究表明,即使是在训练过程中积累了大量知识的大语言模型(LLMs),如ChatGPT和GPT-4,在面对误导性信息时仍可能产生错误信念。研究者为此创建了Farm数据集,以系统性地生成误导信息并测试LLMs在说服性对话中的表现。结果显示,即使是先进如GPT-4,其信念也有20.7%的概率被改变。该研究不仅揭示了LLMs潜在的脆弱性,还提供了评估其鲁棒性的方法,对未来提升LLMs的安全性和准确性具有重要启示作用。论文详细内容可见[此处链接]。
191 5
|
前端开发
前端学习笔记202305学习笔记第二十四天-vue3.0-element icon引入
前端学习笔记202305学习笔记第二十四天-vue3.0-element icon引入
75 0
|
JavaScript 前端开发 API
vue和react的区别是什么
vue和react的区别是什么
142 2
|
API iOS开发 Perl
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
|
数据采集 人工智能 搜索推荐
在线教育行业云上技术服务白皮书-在线教育新模式下对云计算的需求与教育智能硬件-教育智能硬件现状
在线教育行业云上技术服务白皮书-在线教育新模式下对云计算的需求与教育智能硬件-教育智能硬件现状
297 0
|
SQL 消息中间件 存储
SQL、DML和DDL
SQL、DML和DDL
303 0
SQL、DML和DDL
SwiftU—使用Group在多个模拟器中预览视图
SwiftU—使用Group在多个模拟器中预览视图
175 0
SwiftU—使用Group在多个模拟器中预览视图
|
SQL 存储 缓存
一文读懂MySQL查询语句的执行过程
需要从数据库检索某些符合要求的数据,我们很容易写出 Select A B C FROM T WHERE ID = XX 这样的SQL,那么当我们向数据库发送这样一个请求时,数据库到底做了什么?
244 0
一文读懂MySQL查询语句的执行过程