你有3個選擇
- 代表
- 通知
- 座,又稱回調
我想你想要的是代表
假設你有這樣的文件爲lib
TestLib.h
#import <Foundation/Foundation.h>
@protocol TestLibDelegate<NSObject>
-(void)updateCount:(int)count;
@end
@interface TestLib : NSObject
@property(weak,nonatomic)id<TestLibDelegate> delegate;
-(void)startUpdatingCount;
@end
TestLib.m
#import "TestLib.h"
@implementation TestLib
-(void)startUpdatingCount{
int count = 0;//Create count
if ([self.delegate respondsToSelector:@selector(updateCount:)]) {
[self.delegate updateCount:count];
}
}
@end
然後在要使用
#import "ViewController.h"
#import "TestLib.h"
@interface ViewController()<TestLibDelegate>
@property (strong,nonatomic)TestLib * lib;
@end
@implementation ViewController
-(void)viewDidLoad{
self.lib = [[TestLib alloc] init];
self.lib.delegate = self;
[self.lib startUpdatingCount];
}
-(void)updateCount:(int)count{
NSLog(@"%d",count);
}
@end
來源
2015-06-05 08:52:01
Leo
類你看了關於[代表團和通知(HTTPS:/ /developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html#//apple_ref/doc/uid/TP40008195-CH14-SW4)或[使用協議](https:// developer.apple .COM /庫/ IOS /文檔/可可/概念/ ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html#// apple_ref/DOC/UID/TP40011210-CH11)? – Mats