iOS开发-委托(Delegate)浅谈

时间:2022-06-21 23:52:49

委托其实并不是OC中才有,C#中也有,不过彼此的理解方式是不一样的,OC中委托是协议的一种,需要使用@protocol声明,委托一般在iOS开发中页面中传值用的比较多。委托是Cocoa中最简单、最灵活的模式之一,委托其实字面上的意思就是将需要的事情委托给别人做,业务场景可以参考主视图和子视图之间的页面关系,或者是视图层和数据层之间的交互。

简单的委托

委托通过@protocol声明,可以定义方法,引用委托的对象,需要实现其方法,方法默认都是@required的,同时可以设置为可选的@optional,首先定义个委托:

@protocol BookDelegate <NSObject>
@required
- (void)getBookCount;
@optional
- (void)optionMethod; @end

这个时候定义书籍Book类和客户Customer类:

@interface Book : NSObject<BookDelegate>

@end

@interface Customer : NSObject<BookDelegate>

@property (assign,nonatomic) id<BookDelegate> didBookDelegate;

@end

 实现其中的getBookCount方法:

@implementation Book

- (void)getBookCount{
NSLog(@"Book中getBookCount的实现");
} @end @implementation Customer - (void)getBookCount{
NSLog(@"Customer中getBookCount的实现");
} @end

简单的调用:

    Book *book=[[Book alloc]init];

    Customer *customer=[[Customer alloc]init];

    [customer getBookCount];

    [book getBookCount];

  上面几行的代码的结果,不用说大家也能看的懂,接下来看接下来的代码,这个时候大家发现用到了开始顶一个的didBookDelegate:

     customer.didBookDelegate=book;

    [customer.didBookDelegate getBookCount];

 上面就是将Book的实例,Book实现了BookDelegate,这个时候可将Book的实例赋值给customer中的变量,将自己的实例化对象委托给了didBookDelegate。

以上是委托使用基本的场景,作为实例化对象book可以自己执行其方法,也可以通过委托将执行过程转移。

页面传值

简单的就是A页面数据可以传值给B页面,B页面可以传值给A页面,简单的两个页面传值,页面参考如下:

iOS开发-委托(Delegate)浅谈

都是文本框和按钮,跳转方式选取的时Modal:

第一个页面ViewController的定义:

#import <UIKit/UIKit.h>
#import "SecondViewController.h" @interface ViewController : UIViewController<StudySubjectDelegate> @property (strong, nonatomic) IBOutlet NSString *firstData; @property (weak, nonatomic) IBOutlet UITextField *subjectName; @end

 第二个页面SecondViewController头文件的定义,并且声明了一个委托:

//
// SecondViewController.h
// Sample
//
// Created by keso on 15/2/3.
// Copyright (c) 2015年 keso. All rights reserved.
// #import <UIKit/UIKit.h> @class SecondViewController;
@protocol StudySubjectDelegate <NSObject> - (void)shouldChangeValue:(SecondViewController*)controller; @end @interface SecondViewController : UIViewController @property (assign,nonatomic) id<StudySubjectDelegate> firstViewDelegate; @property (weak, nonatomic) IBOutlet NSString *showData; @property (weak, nonatomic) IBOutlet UITextField *studySubject; @end

 ViewController.m中的点击事件:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"firstEdit"]) {
SecondViewController *controller=segue.destinationViewController;
NSLog(@"%@",self.subjectName.text);
//将自己本身的实例传递给第二个视图
controller.firstViewDelegate=self;
controller.showData=self.subjectName.text;
}
}

 赋值的方式还可以是这样的,其实中的key就是第二个视图定义的属性:

        if ([controller respondsToSelector:@selector(setShowData:)]) {
[controller setValue:self.subjectName.text forKey:@"showData"];
}

第二个页面的点击事件就比较简单了,代码如下:

    [self.firstViewDelegate shouldChangeValue:self];

上面中其实可以简单的看到oc中的委托就是将自己的实例交给其他对象的成员变量,然后由其成员变量执行实例的工作,的今天不知道为什么有点头疼,说个事情就是最后的第二个页面跳转到一个页面可以接收到值,无法给UITextField赋值,暂时没有搞明白如何才能赋值上去,每次进入就变成了null,有知道可以指点一下,多谢~