将 UIViewController 设置为另一个 UIViewController 的属性是不是一个坏主意?
例如,假设我有一个 RootViewController
类和 AnotherViewController
类,并且我需要将 RootViewController
中的属性从 AnotherViewController< /code>...在
AnotherViewController.h
中拥有“RootViewController”属性是否安全(以便我可以访问其实例变量)?
@interface AnotherViewController : UIViewController {
RootViewController *rootViewController;
}
@property (nonatomic, retain) RootViewController *rootViewController;
@end
@implementation AnotherViewController
@synthesize rootViewController;
- (void)someMethod {
// set the data was added flag, so the rootViewController knows to scroll to the bottom of the tableView to show the new data
self.rootViewController.dataWasAdded = YES;
// if the user came in via a search result, make the search controller's tableView go away
self.rootViewController.searchDisplayController.active = NO;
}
如果这不是一个好主意,有人可以解释为什么吗?
在上面的代码中,我知道我可以使用协议/委托来处理同样的事情 - 我猜我可能应该这样做。然而,我读过的书籍或其他材料都没有真正讨论过这一点。
我问的原因是我正在使我的应用程序通用,并使用 UISplitViewController
我注意到我需要经常更新我的“主视图”作为用户“详细视图”中的更改。因此,我采取了看似简单的方法并开始将 UIViewControllers 设置为属性...但我遇到了一些难以跟踪的内存泄漏和偶尔的崩溃。我读到了一些有关“循环引用”的内容,想知道这是否可能是问题的一部分(我确实有几个地方将 UIViewControllers 设置为彼此的属性)。
感谢您提供任何见解或指向涵盖此内容的参考材料。
For example, say I have a RootViewController
class and AnotherViewController
class, and I need to change a property in my RootViewController
from AnotherViewController
... is it safe to have a "RootViewController" property in AnotherViewController.h
(so that I can access its instance variables)?
@interface AnotherViewController : UIViewController {
RootViewController *rootViewController;
}
@property (nonatomic, retain) RootViewController *rootViewController;
@end
@implementation AnotherViewController
@synthesize rootViewController;
- (void)someMethod {
// set the data was added flag, so the rootViewController knows to scroll to the bottom of the tableView to show the new data
self.rootViewController.dataWasAdded = YES;
// if the user came in via a search result, make the search controller's tableView go away
self.rootViewController.searchDisplayController.active = NO;
}
If that's not a good idea, can anybody explain why?
In the code above, I know I could have used a protocol/delegate to handle the same thing - and I'm guessing I probably should. However, none of the books or other materials I've read has really discussed this.
The reason I'm asking is that I'm in the process of making my app universal, and using a UISplitViewController
I've noticed that I need to often update my "master view" as the user makes changes in the "detail view". So, I took what seemed the easy route and started setting UIViewControllers
as properties... but I'm experiencing some hard to track memory leaks and occasional crashes. I read something about "circular references", and wonder if that could be part of the issue (I do have a couple of places where UIViewControllers
are set as properties of one another).
Thanks for any insight, or pointers to reference materials that cover this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会避免养成这种习惯,因为有更好更安全的选择。使用协议/委托是 Apple 跨类管理数据的首选方式。您还可以设置 NSNotifications 以将数据/事件从一个类发送/触发到另一个类。键值观察(KVO)也是监听变化的好方法。
在 MVC 结构中,子视图和下游控制器确实不应该知道(也称为保留引用)其父视图。父母管理和跟踪孩子的方式应该始终相反。
I'd avoid making a habit of this as there are better safer alternatives. Using a protocol/delegate is the preferred Apple way of managing data across classes. You can also set up NSNotifications to send/trigger data/events from one class to another. Key Value Observing (KVO) is also a decent way to listen in for changes.
In MVC structuring, the child views and downstream controllers really should have no idea (aka, keeping references) of their parents. It should always work the other way around where the parents manage and keep track of the children.