博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UI 03 协议代理
阅读量:6877 次
发布时间:2019-06-26

本文共 2676 字,大约阅读时间需要 8 分钟。

//

//  RootViewController.m

//  ui - 04 协议代理!!

//

//  Created by dllo on 15/11/11.

//  Copyright (c) 2015 dllo. All rights reserved.

//

#import "RootViewController.h"

#import "RootView.h"

#warning 步骤 关联协议方法(签协议)

@interface RootViewController () <RootViewControllDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    

    

    RootView *view1 = [[RootView alloc]initWithFrame:CGRectMake(10, 100, 300, 500)];

    

#warning 步骤 设置代理人

    view1.delegate = self;

    [self.view addSubview:view1];

    view1.backgroundColor = [UIColor yellowColor];

    [view1 release];

    

    // Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

#warning 步骤6 - 实现协议方法(订制指定的功能)

- (void)changeVCbackground {

    self.view.backgroundColor = [UIColor orangeColor];

    NSLog(@"余浩");

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end

!!!!Rootview中

//

//  RootView.h

//  ui - 04 协议代理!!

//

//  Created by dllo on 15/11/11.

//  Copyright (c) 2015 dllo. All rights reserved.

//

#import <UIKit/UIKit.h>

#warning 步骤3 - 关联协议方法

@protocol RootViewControllDelegate <NSObject>

- (void) changeVCbackground;

@end

@interface RootView : UIView

#warning 步骤2 创建代理

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

@end

//

//  RootView.m

//  ui - 04 协议代理!!

//

//  Created by dllo on 15/11/11.

//  Copyright (c) 2015 dllo. All rights reserved.

//

#import "RootView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        [self createSubview];

    }

    return self;

}

- (void)createSubview

{

    self.backgroundColor = [UIColor blueColor];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];

    button1.frame = CGRectMake(330, 50, 100, 100);

    button1.backgroundColor = [UIColor redColor];

    [button1 setImage:[UIImage imageNamed:@"login_btn_normal@2x.png"] forState:UIControlStateHighlighted];

    [button1 setImage:[UIImage imageNamed:@"login_btn_press@2x副本.png"] forState: UIControlStateNormal];

    [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:button1];

}

- (void)buttonAction:(UIButton *)sender

{

#warning  步骤 1 使用代理调用协议方法

    [self.delegate changeVCbackground];

    

}

@end

转载于:https://www.cnblogs.com/yuhaojishuboke/p/5043087.html

你可能感兴趣的文章
一体化解决方案是一条正确的发展道路
查看>>
Java中final和static关键字总结
查看>>
Oracle11.2.0.4-Rac集群hang分析记录
查看>>
Objective-C中init函数实现的相关研究
查看>>
java中重载与重写的区别
查看>>
Powerdesigner数据库建模--概念模型--ER图
查看>>
Rss内容读取
查看>>
解决Xcode 代码颜色不显示
查看>>
java 的继承,深入理解
查看>>
一个强大的人民币大写转换的正则表达式
查看>>
MySQL索引背后的数据结构及算法原理zz
查看>>
用代码实现Sharepoint2010的个人信息的照片上传(原创)
查看>>
MF前传——探索者一号简介
查看>>
HDU 3681 Prison Break
查看>>
Spring-模板方法模式及Callback
查看>>
牛刀小试:使用Reactive Extensions(Rx),一行代码实现多线程任务执行规定时间后自动停止...
查看>>
addslashes与mysql_real_escape_string的区别
查看>>
[译]XPath和CSS选择器
查看>>
X皮书之shell热身
查看>>
js实现HTML标题栏中新消息提示效果
查看>>