那麼你可以在你的ModelClass.m這樣做有兩種方式 1.代表 2. NSNotificationCenter
在模型class.h文件
@protocol someProtoColName<NSObject>
-(void)finishedDoingMyStuff;
@end
@interface ModelClass()
@property (nonatomic,weak) id<someProtoColName> delegateObj;
然後
-(void)someactionHappend{
//this is the method where you save your things call the delegate method inside here like this.
[self.delegateObj finishedDoingMyStuff];
}
然後在你的CallerClass.h中
#import ModelClass.h
@interface CallerClass:UIViewController<someProtoColName>
內部CallerClass.m viewDidLoad
-(void)viewDidLoad{
ModelClass *model = [[ModelClass alloc]init];
model.delegateObj = self;
}
//Now declare the delegate method
-(void)finishedDoingMyStuff{
//update your code that this has happend. this will be called when your model class's button action inside which you sent the `self.delegateObj`message is invoked instantaneously.
}
- NSNotificationCenter
CallerClass.m
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(someMethod) name:@"NOTIFICATIONNAME" object:nil];
}
-(void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"NOTIFICATIONNAME" object:nil];
}
-(void)someMethod{
//something has happened, do your stuff
}
並且在每個模型類的(如果他們是很多或一個dsnt的事情)| ModelClass.m
-(void)someactionHappend{
//this is your action method that you want to do stuff in the model
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONNAME" object:nil userInfo:nil];
}
希望這可以幫助你。
如果我想快速做到這一點? –
相同的機械裝置只需swift語法 –