简体   繁体   English

IOS目标C祖父母或外祖父母

[英]IOS Objective C Grandparent as delegate

I have a "GrandParentViewController" that has all methods of my application. 我有一个“ GrandParentViewController”,其中包含我应用程序的所有方法。 I have a subview "ParentViewController" that contains a few subviews "ChildVC" for which I want the methods to be delegated to the "GrandParentViewController". 我有一个子视图“ ParentViewController”,其中包含一些子视图“ ChildVC”,我希望将这些方法委托给“ GrandParentViewController”。 I currently have a delegate chain from the childVC >> parentVC >> GrandparentVC but it feels wrong. 我目前有一个来自childVC >> parentVC >> GrandparentVC的委托链,但是感觉不对。 What is the IOS way of doing this? IOS这样做的方式是什么?

It seems that single responsibility principle is violated in your code. 您的代码似乎违反了单一责任原则 Maybe it is a good idea to split GrandParentViewController to several classes? 将GrandParentViewController拆分为几个类也许是个好主意?

Also make sure you know what is protocol in Objective-C and how it should be used. 还要确保您知道什么是Objective-C 协议以及应如何使用它。

I do not recommend to use NSNotifications since it is good and was designed for other cases then direct delegation. 我不建议使用NSNotifications,因为它很好,并且是为其他情况设计的,然后直接委托。

I wouldn't say that there's a set iOS way. 我不会说有一种固定的iOS方式。 I do something similar to what you described because it's the natural way that my VCs should be related. 我做的事情与您描述的类似,因为这是我的VC之间自然联系的方式。 Go with whatever paradigm helps you keep your code clean for future changes, modularized for reusability, and of course, functional. 使用任何范式都可以帮助您保持代码清洁以备将来更改,模块化以实现可重用性以及功能。

I will say that if any communications between VCs don't fit with your paradigm, don't force it. 我会说,如果VC之间的任何通信都不符合您的范式,请不要强行使用它。 Use NSNotifications instead. 请改用NSNotifications。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM