应用程序委托分配的对象显示在多个子视图中? [英] App delegate allocated object displayed in multiple subviews?

查看:81
本文介绍了应用程序委托分配的对象显示在多个子视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有点不高兴..构建一个包含两个视图的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.

来自

应用程序委托是在应用程序启动时创建的自定义对象, 通常由UIApplicationMain函数实现.这个的主要工作 目的是处理应用程序内的状态转换.例如, 该对象负责启动时的初始化和处理 与背景之间的过渡.

The app delegate is a custom object created at app launch time, usually by the UIApplicationMain function. The primary job of this object is to handle state transitions within the app. For example, this object is responsible for launch-time initialization and handling transitions to and from the background.

引用App Delegate进行其他任何操作都类似于使用全局变量.乍一看似乎很容易捷径,但是随着代码库的增长,它很快就会导致混乱.

Referring to the App Delegate for anything else is like using global variables. It seems like an easy short-cut at the start, but can quickly end up a confusing mess as your code base grows.

应用程序委托就是它所说的-UIApplication的委托对象.您正在创建的视图与UIApplication无关,因此不应在它的委托附近.

The app delegate is what it says - the delegate object of UIApplication. The views you're creating have nothing to do with UIApplication, so shouldn't be anywhere near it's delegate.

设计和构建鼓励重用的简单自包含类.从长远来看,您会很高兴的.

Design and build simple, self contained classes that encourage re-use. In the long term you'll be glad you did.

通过创建单个rivbox,您会感觉到什么优势?

What advantage do you perceive by creating this single rivbox?

这篇关于应用程序委托分配的对象显示在多个子视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆