应用程序委托分配的对象显示在多个子视图中? [英] App delegate allocated object displayed in multiple subviews?
问题描述
我在这里有点不高兴..构建一个包含两个视图的iOS游戏:BaseView和MapView.现在,这些当然具有相同的appDelegate了.
I'm in a bit of a pickle here.. Building an iOS game that contains lets say two views: BaseView and MapView. Now, these of course has the same appDelegate.
这两个都使用名为 rivbox 的UIView子类.在较早的版本中,我在Base视图和Map视图中都分配了rivbox实例.当我意识到使用该rivbox将拥有很多子视图时,我改为选择在appDelegate中分配仅rivbox的一个实例,然后,在加载子视图时可以借用"通过使用这些漂亮的功能,可以从appDelegate获得rivbox.
These both use an UIView subclass called rivbox. In earlier builds, I allocated an instance of rivbox in both the Base and the Map-view. When I realized that I'll have a lot of subviews using this rivbox I instead chose to allocate just ONE instance of rivbox in the appDelegate and then, when a subview is loaded is gets to "borrow" the rivbox from the appDelegate by using these nifty functions.
-(void)viewDidLoad {
//Get all the sweets from the parent!
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
rivbox = appDelegate.rivbox;
[self.view addSubview:rivbox];
...
}
如果再次返回该视图,此功能用于更新所有权
And this function is for updating the ownership if we return to this view again
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(rivbox != nil) {
[rivbox setDelegate:self];
[self.view addSubview:rivbox];
}
}
这很好!我在appDelegate中找到了rivbox,它已经更新了!当我移到另一个视图时,它也可以在那里加载!但是当我回到第一个视图时,这似乎在泄漏?!?!回到原来的视图时,即使以前的视图已拥有它,我也无法在其先前的视图中看到该rivbox!
This works fine! I find the rivbox in the appDelegate and it's updated! When I move to another view it loads there too just fine! But when I move back to the first view this seems to be leaking?!?! I can't see the rivbox in my previous view when I return to it even though the previous view now owns it!
问题是:这有什么问题?每次返回已加载的视图时,是否真的必须将rivbox添加为子视图吗?如果我删除了该字符串,那么返回前一个视图时将看不到MyBox.
Problem is: What is wrong with this? Do I really have to add rivbox as a subview every time I return to a view that's already been loaded? If i remove that string then I can't see the MyBox when I return to a prior view.
推荐答案
这确实是一种糟糕的做法.如果您在任何类中都引用应用程序委托,则可能需要重新考虑您在做什么.
This is really poor practice. If you're referring to the app delegate in any class you probably need to rethink what you're doing.