-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db87ad2
commit 427c268
Showing
6 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# | ||
# Be sure to run `pod spec lint MultiDelegateOC.podspec' to ensure this is a | ||
# valid spec and to remove all comments including this before submitting the spec. | ||
# | ||
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html | ||
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/ | ||
# | ||
|
||
Pod::Spec.new do |s| | ||
s.name = "MultiDelegateOC" | ||
s.version = "0.0.1" | ||
s.summary = "It can provide a multi delegate model for iOS and OSX." | ||
s.homepage = "https://github.com/FlameGrace/MultiDelegateOC" | ||
s.license = "BSD" | ||
s.author = { "FlameGrace" => "flamegrace@hotmail.com" } | ||
s.ios.deployment_target = '7.0' | ||
s.osx.deployment_target = '10.9' | ||
s.watchos.deployment_target = '2.0' | ||
s.tvos.deployment_target = '9.0' | ||
s.source = { :git => "https://github.com/FlameGrace/MultiDelegateOC.git", :tag => "0.0.1" } | ||
s.source_files = "MultiDelegateOC", "MultiDelegateOC/**/*.{h,m}" | ||
s.public_header_files = "MultiDelegateOC/**/*.h" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// MDWeakObject.h | ||
// Combin | ||
// | ||
// Created by Flame Grace on 2017/6/26. | ||
// Copyright © 2017年 flamegrace. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface MDWeakObject : NSObject | ||
|
||
@property (weak, nonatomic) id object; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// MDWeakObject.m | ||
// Combin | ||
// | ||
// Created by Flame Grace on 2017/6/26. | ||
// Copyright © 2017年 flamegrace. All rights reserved. | ||
// | ||
|
||
#import "MDWeakObject.h" | ||
|
||
@implementation MDWeakObject | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// NSObject+MultiDelegate.h | ||
// MultiDelegate | ||
// | ||
// Created by Flame Grace on 16/11/17. | ||
// Copyright © 2016年 flamegrace@hotmail.com. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
//获取单个代理对象对其进行操作 | ||
typedef void(^MultiDelegateOperateBlack)(id delegate); | ||
|
||
|
||
/* | ||
此类扩展能实现多代理 | ||
需要委托代理执行方法时,调用operateDelegates:方法可在相应block块中获取到单个代理,对其进行操作 | ||
*/ | ||
@interface NSObject (MultiDelegate) | ||
|
||
//添加一个代理 | ||
- (void)addMultiDelegate:(id)delegate; | ||
//移除一个代理 | ||
- (void)removeMultiDelegate:(id)delegate; | ||
//操作代理 | ||
- (void)operateMultiDelegates:(MultiDelegateOperateBlack)operate; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// NSObject+MultiDelegate.m | ||
// MultiDelegate | ||
// | ||
// Created by Flame Grace on 16/11/17. | ||
// Copyright © 2016年 flamegrace@hotmail.com. All rights reserved. | ||
// | ||
|
||
#import "NSObject+MultiDelegate.h" | ||
#import "MDWeakObject.h" | ||
#import <objc/runtime.h> | ||
|
||
|
||
@implementation NSObject (MultiDelegate) | ||
|
||
//使用动态运行时添加数组对象multiDelegates,保存代理 | ||
- (void)setMultiDelegates:(NSMutableArray<MDWeakObject *> *)multiDelegates | ||
{ | ||
objc_setAssociatedObject(self, @selector(multiDelegates), multiDelegates, OBJC_ASSOCIATION_RETAIN); | ||
} | ||
|
||
- (NSMutableArray<MDWeakObject *> *)multiDelegates | ||
{ | ||
NSMutableArray *multiDelegates = objc_getAssociatedObject(self,@selector(multiDelegates)); | ||
if(multiDelegates == nil) | ||
{ | ||
multiDelegates = [[NSMutableArray alloc]init]; | ||
objc_setAssociatedObject(self, @selector(multiDelegates), multiDelegates, OBJC_ASSOCIATION_RETAIN); | ||
} | ||
return multiDelegates; | ||
} | ||
|
||
- (void)addMultiDelegate:(id)delegate | ||
{ | ||
if(delegate == nil)return; | ||
|
||
__block BOOL exist = NO; | ||
|
||
__weak typeof(self) weakSelf = self; | ||
|
||
[self.multiDelegates enumerateObjectsUsingBlock:^(MDWeakObject * _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) { | ||
if(delegate.object) | ||
{ | ||
if([delegate.object isEqual:delegate]) | ||
{ | ||
exist = YES; | ||
} | ||
} | ||
else | ||
{ | ||
[weakSelf.multiDelegates removeObject:delegate]; | ||
} | ||
}]; | ||
|
||
if(!exist) | ||
{ | ||
MDWeakObject *newDelegate = [[MDWeakObject alloc]init]; | ||
newDelegate.object =delegate; | ||
[self.multiDelegates addObject:newDelegate]; | ||
} | ||
|
||
} | ||
|
||
- (void)removeMultiDelegate:(id)delegate | ||
{ | ||
if(delegate == nil)return; | ||
|
||
__weak typeof(self) weakSelf = self; | ||
|
||
[self.multiDelegates enumerateObjectsUsingBlock:^(MDWeakObject * _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) { | ||
if([delegate.object isEqual:delegate]) | ||
{ | ||
[weakSelf.multiDelegates removeObject:delegate]; | ||
} | ||
}]; | ||
|
||
} | ||
|
||
- (void)operateMultiDelegates:(MultiDelegateOperateBlack)operate | ||
{ | ||
[self.multiDelegates enumerateObjectsUsingBlock:^(MDWeakObject * _Nonnull delegate, NSUInteger idx, BOOL * _Nonnull stop) { | ||
operate(delegate.object); | ||
}]; | ||
} | ||
|
||
|
||
|
||
@end |