您的位置:首页 > 其它

关于协议传值

2016-02-18 17:21 211 查看
// .h 内部的声明部分 (即:fourthController )

//1 声明协议

//UI中的协议名称为,当前类名 + Delegate

@protocol FourthViewControllerDelegate <NSObject>

//不加说明:默认是必须实现的方法

- (void)pushValue:(NSString *)text uicolor:(UIColor *)color;

@end

@interface FourthViewController : UIViewController

//2 声明协议的代理对象

@property (nonatomic, assign) id <FourthViewControllerDelegate> delegate;//代理对象

@end

// .m 内部的实现

// 3, 执行协议的方法 (写在具体需要传值的方法内部)

if (self.delegate != nil && [self.delegate respondsToSelector:@selector(pushValue: uicolor:)] ) { //代理不为空 代理内容接受

[self.delegate pushValue:self.field.text uicolor:self.view.backgroundColor];//代理执行此方法,并且此时实现需要传的值

}

// 接收传值的内部 的 实现部分

// 4 接受代理

@interface ThirdViewController : UIViewController<FourthViewControllerDelegate>

// 5 指定代理对象为当前的视图控制器

fourthController.delegate = self;

// 6 实现声明的协议方法

//6 实现协议方法(写在当前视图控制器的 .m 内部)

- (void)pushValue:(NSString *)text uicolor:(UIColor *)color{

self.label.text = text;

self.view.backgroundColor = color;

}

注意:协议传值的 六步 一定要指定代理对象,并且声明代理对象的语义类型是 assign 类型的;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: