【发布时间】:2011-06-30 14:14:00
【问题描述】:
例如,假设我有一个 RootViewController 类和 AnotherViewController 类,我需要将我的 RootViewController 中的属性从 AnotherViewController... AnotherViewController.h(这样我就可以访问它的实例变量)?
@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 设置为彼此的属性)。
感谢您提供任何见解或指向涵盖此内容的参考资料。
【问题讨论】:
标签: ios uiviewcontroller cocoa-design-patterns