我想更新应用程序中模态视图覆盖的视图控制器,我使用swift。我现在的目的是:
右边的视图是调用者,左边的视图是第一个按钮触发的模态视图。他们进行基本编辑,添加新操作。这些视图以模态方式呈现在主视图上,我想要做的是在保存其中一个模态的数据后更新两个容器中嵌入的表视图控制器。
我研究了一个viewLoad事件的使用,但我现在有点卡住了。我怎么能这样做?
答案 0 :(得分:0)
你可以用两种方式做到这一点 1.代表 2. NSNotificationCenter
在您的Model class.h文件中
@protocol someProtoColName<NSObject>
-(void)finishedDoingMyStuff;
@end
@interface ModelClass()
@property (nonatomic,weak) id<someProtoColName> delegateObj;
然后在你的ModelClass.m
中-(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.
}
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
}
并且在每个Model类中(如果它们是多个或一个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];
}
希望这可以帮助你。