iOS设计模式——委托

来源:互联网 发布:淘宝背景音乐怎么关闭 编辑:程序博客网 时间:2024/06/03 05:40

iOS设计模式简单实用(界面传值)

委托(delegate)也叫代理是iOS开发中常用的设计模式。我们借助于protocol(参考博文:objective-c协议(protocol))可以很方便的实现这种设计模式。

什么是代理?

苹果的官方文档给了很清晰的解释:
Delegation is a simple and powerful pattern in which one object in a program acts on behalf of, or in coordination with, another object. The delegating object keeps a reference to the other object—the delegate—and at the appropriate time sends a message to it. The message informs the delegate of an event that the delegating object is about to handle or has just handled. The delegate may respond to the message by updating the appearance or state of itself or other objects in the application, and in some cases it can return a value that affects how an impending event is handled. The main value of delegation is that it allows you to easily customize the behavior of several objects in one central object.
意译一下就是:代理是一种简单而功能强大的设计模式,这种模式用于一个对象“代表”另外一个对象和程序中其他的对象进行交互。 主对象(这里指的是delegating object)中维护一个代理(delegate)的引用并且在合适的时候向这个代理发送消息。这个消息通知“代理”主对象即将处理或是已经处理完了某一个事件。这个代理可以通过更新自己或是其它对象的UI界面或是其它状态来响应主对象所发送过来的这个事件的消息。或是在某些情况下能返回一个值来影响其它即将发生的事件该如何来处理。代理的主要价值是它可以让你容易的定制各种对象的行为。注意这里的代理是个名词,它本身是一个对象,这个对象是专门代表被代理对象来和程序中其他对象打交道的。

自定义代理

首先,我们创建两个UIViewController分别命名为A_ViewController和B_ViewController。我们要做的功能是,在B_ViewController的TextField里输入字符串,点击SAVE按钮,把这条字符串显示在A_ViewController里的TextView上。

AViewController

BViewController

具体步骤:

1.在B_ViewController.h中声明一个协议ChangeTextViewDelegate;并且添加一个代理的声明。

#import <UIKit/UIKit.h>//声明一个protocol@protocol ChangeTextViewDelegate <NSObject>- (void) textEntered:(NSString*) text;@end@interface SecondVC : UIViewController@property (assign, nonatomic) id <ChangeTextViewDelegate> delegate;//声明属性@end

2.实现SAVE按钮的方法,当点击SAVE按钮,会返回A_ViewController,并改变A中TextView的字符。

//SAVE按钮- (IBAction)saveBtnClick:(id)sender {    //Is anyone listening    if([delegate respondsToSelector:@selector(textEntered:)])    {//只有当代理存在,且textEntered方法被实现的时候才执行下面的语句        //send the delegate function with the amount entered by the user        [delegate textEntered:self.myTextField.text];    }    [self dismissViewControllerAnimated:YES completion:^{}];}

[delegate textEntered:textEntered.text];这句代码的含义就是ChangeTextViewController通知代理,textEntered这个事件发生了,对textEntered这个消息的实现,即如何响应这个textEntered的事件由代理来实现。在本例中,A_ViewController就是B_ViewController对象的代理。所以,我们要对A_ViewController做相应的设置使其满足代理的条件。首先,在A_ViewController.h或者.m中声明遵循协议ChangeTextViewDelegate。然后编辑GoToNext按钮的响应函数-(IBAction)ChangeTextBtnClick:(id)sender;

3.在A_ViewController.m文件中声明遵循协议ChangeTextViewDelegate。

@interface ViewController ()<ChangeTextViewDelegate>

4.实现GoToNext按钮的处理事件

- (IBAction)ChangeTextBtnClick:(id)sender {    SecondVC * secondVC =[[SecondVC alloc]initWithNibName:@"SecondVC" bundle:nil];    secondVC.delegate=self;    [self presentViewController:secondVC animated:YES completion:^{        ;    }];}

5.实现代理方法

#pragma mark --ChangeTextDelegate-(void)textEntered:(NSString *)text{    self.myTextViewXX.text=text;}

注意,CTViewController.delegate = self;这句实现了SecondViewController成为ChangeTextViewController对象的代理。

本文对应的源代码下载链接:本博文源代码Demo

本文若有不当之处,还望不吝赐教!

0 0