iOS开发 delegate,SEL和Block
来源:互联网 发布:获取端口号 编辑:程序博客网 时间:2024/05/15 02:06
iOS开发 delegate,SEL和Block
在Object-c中解决子类和父类之间事件传递,不同根类之间事件传递的三种方法
由于Object-c是单向继承的,父类 向子类传事件,数据,可用点语法,或者直接调用成员函数即可,
但是相反方向则需要引入委托,回调这样的概念
下面我将给出继承UIview,为他的子类添加点击事件,让他去改变父视图的背景色这个简单的例子的三种实现方法:
//
// MyView.h
// myView
//
// Created by mac_shun on 13-9-7.
// Copyright (c) 2013年 wangshunPush. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol MyViewDelegate;
@interface MyView :UIView
{
id <MyViewDelegate> _delegate;
}
@property (nonatomic,retain) id <MyViewDelegate> delegate;
@end
@protocol MyViewDelegate <NSObject>
-(void)changeColor;
@end
//
// MyView.m
// myView
//
// Created by mac_shun on 13-9-7.
// Copyright (c) 2013年 wangshunPush. All rights reserved.
//
#import "MyView.h"
@implementation MyView
@synthesize delegate =_delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
//允许触发
self.userInteractionEnabled =YES;
}
return self;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.delegate && [self.delegate respondsToSelector:@selector(changeColor)]) {
// 让 delegate 去掉 自己实现的 changeColor 方法
[self.delegate performSelector:@selector(changeColor)];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
#import <UIKit/UIKit.h>
#import "MyView.h"
@interface ViewController :UIViewController<MyViewDelegate>
@end
//
// ViewController.m
// myView
//
// Created by mac_shun on 13-9-7.
// Copyright (c) 2013年 wangshunPush. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =[UIColorscrollViewTexturedBackgroundColor];
MyView *v =[[MyViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
v.backgroundColor = [UIColorredColor];
v.delegate =self;
[self.viewaddSubview:v];
}
-(void)changeColor//协议方法
{
self.view.backgroundColor = [UIColorunderPageBackgroundColor];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface MyView :UIView
{
id delegate;
SEL method;
}
-(void)addTarget:(id) d action:(SEL) m;
@end
//
// MyView.m
// myView
//
// Created by mac_shun on 13-9-7.
// Copyright (c) 2013年 wangshunPush. All rights reserved.
//
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
//允许触发
self.userInteractionEnabled =YES;
}
return self;
}
-(void)addTarget:(id)d action:(SEL)m
{
delegate = d;
method = m;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (delegate && [delegaterespondsToSelector:method]) {
//delegate 调 method 方法
[delegateperformSelector:methodwithObject:self];
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =[UIColorscrollViewTexturedBackgroundColor];
MyView *v =[[MyViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
v.backgroundColor = [UIColorredColor];
[vaddTarget:selfaction:@selector(changeColorForSEL:)];
[self.viewaddSubview:v];
}
-(void)changeColorForSEL:(MyView*)view
{
self.view.backgroundColor = [UIColorunderPageBackgroundColor];
}
#import <UIKit/UIKit.h>
@interface MyView :UIView
{
//定义一个指针变量 getMethod,这个指针指向一个函数
void (^getMethod)(MyView*);
}
/*
为MyView添加一个方法
形参 m
形参类型 为一个函数的地址 void (^)(MyView*)
*/
-(void)addBlockMethod:(void (^)(MyView* view)) m;
@end
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
//允许触发
self.userInteractionEnabled =YES;
}
return self;
}
-(void)addBlockMethod:(void (^)(MyView * view))m
{
//获得外部函数的地址
[getMethod release];
getMethod = [mcopy];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (getMethod) {//当触发view时调用已经指向的函数
getMethod(self);
}
}
@end
#import <UIKit/UIKit.h>
#import "MyView.h"
@interface ViewController :UIViewController
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor =[UIColorscrollViewTexturedBackgroundColor];
MyView *v =[[MyViewalloc]initWithFrame:CGRectMake(0,0,100,100)];
v.backgroundColor = [UIColorredColor];
[vaddBlockMethod:^(MyView *view) {
self.view.backgroundColor =[UIColorunderPageBackgroundColor];
}];
[self.viewaddSubview:v];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- iOS开发 delegate、 SEL和Block
- iOS开发 delegate,SEL和Block
- [ios] delegate, selector和block
- iOS开发Delegate,Notification,Block使用心得
- ios 委托(delegate)和block 对比
- iOS 学会使用delegate和block<一>
- iOS 学会使用delegate和block<二>
- iOS Block 和 Delegate 的使用比较
- 【iOS开发】SEL和Selector 原理小结
- iOS开发中 delegate和 block传值方法的比较
- iOS 开发 是该选择Block 还是Delegate呢
- IOS:两种回调的方式实现(delegate和block)
- iOS-Delegate-Block-NSNotification的区别和使用
- iOS中block和delegate的区别(面试必问)
- OC--SEL和Block
- iOS开发之SEL用法
- IOS开发- SEL知识回顾
- description 方法 和 SEL - iOS
- 关于游戏框架设计的搜索(2)
- ios学习之UISwipeGestureRecognizer手势识别
- C语言练习-指针(数组,字符串)
- 博客搬家通知!!!
- C 语言程序设计实践 4.3 龟兔赛跑
- iOS开发 delegate,SEL和Block
- Android使用Asynchronous Http Client完成登录保存cookie的问题
- [leetCode] Regular Expression Matching
- 【LeetCode】Merge Two Sorted Lists
- Visual Studio 2013 C/C++单元测试
- 学生管理系统源代码
- C 语言程序设计实践 4.4 车牌号
- ios开发——用UITableView展示多行多列图片
- 分布式爬虫系统