我在设计中依赖 self.parentViewController 和/或 self.presentingViewController 这通常是一个坏兆头
我有一个 SubSelectVC
来处理从 SearchVC
模态呈现的子选择选择。 SubSelectVC
有一个 -(void)didSelectRowAtIndexPath
来执行这些选项,大致如下:
if ([[[UIDevice currentDevice] systemVersion] intValue] < 5) {
((SearchVC *)self.parentViewController.filters.filterValue = @"Some value";
}
else {
((SearchVC *)self.presentingViewController.filters.filterValue = @"Some value";
}
这看起来像是糟糕的设计,但我的意思是,可以选择这样做就在那里,而且非常简单!这是什么问题,我该如何纠正? (我应该使用委托吗?)
I have a SubSelectVC
that handles sub-selection choice that is presented modally from a SearchVC
. The SubSelectVC
has a -(void)didSelectRowAtIndexPath
that performs these options, roughly:
if ([[[UIDevice currentDevice] systemVersion] intValue] < 5) {
((SearchVC *)self.parentViewController.filters.filterValue = @"Some value";
}
else {
((SearchVC *)self.presentingViewController.filters.filterValue = @"Some value";
}
This seems like it screams of bad design but, I mean, the option to do it this way is there and it's so easy! What's wrong with this, and how would I make it right? (Should I use delegation?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我认为更好的封装版本是在 SubSelectVC 的头文件中定义委托协议,并在 SubSelectVC 上定义委托属性。
这样,您的视图控制器就可以重复用于任何需要从列表中进行模式选择的任务。
编辑:添加示例头:
SubSelectVC.h:
Yes, I think a better encapsulated version of this would be to define a delegate protocol in the header file for SubSelectVC, and a delegate property on SubSelectVC.
That way your view controller is reusable for any task that requires modal selection from a list.
EDIT: added example header:
SubSelectVC.h: