简体   繁体   English

不从子类调用[super viewDidLoad]和[super dealloc]

[英]Not calling [super viewDidLoad] and [super dealloc] from a subclass

I have these 3 classes: 我有这三个班:

@interface BaseViewController : UIViewController
@interface BaseWebViewController : BaseViewController
@interface SecondaryWebViewController : BaseViewController

In AppDelegate, I am setting the rootViewController to be BaseWebViewController which will never be de-alloced throughout the entire lifetime of the app. 在AppDelegate中,我将rootViewController设置为BaseWebViewController,它永远不会在应用程序的整个生命周期内取消分配。 In BaseWebViewController, I am calling [super viewDidLoad] and [super dealloc] in their respective methods. 在BaseWebViewController中,我在各自的方法中调用[super viewDidLoad]和[super dealloc]。

Throughout most of the app, the user will be navigating through the UIWebView found on BaseWebViewController. 在整个应用程序的大部分内容中,用户将浏览BaseWebViewController上的UIWebView。 But on certain links, I need to push (and later, pop out of) SecondaryWebViewController. 但是在某些链接上,我需要推送(后来,弹出)SecondaryWebViewController。

My question is that since both BaseWebViewController and SecondaryWebViewController are subclassing the BaseViewController, I'm not sure what to do regarding calling [super viewDidLoad] and [super dealloc] in SecondaryWebViewController. 我的问题是,由于BaseWebViewController和SecondaryWebViewController都是BaseViewController的子类,我不知道如何在SecondaryWebViewController中调用[super viewDidLoad]和[super dealloc]。

In SecondaryWebViewController, I had been calling those [super viewDidLoad] and [super dealloc], but I noticed it caused some problems especially with random crashes when calling [super dealloc]. 在SecondaryWebViewController中,我一直在调用那些[super viewDidLoad]和[super dealloc],但是我注意到它在调用[super dealloc]时引起了一些问题,尤其是随机崩溃。 Since BaseWebViewController must always be present throughout the entire lifetime of the app, I don't ever want dealloc called on the BaseViewController. 由于BaseWebViewController必须始终存在于应用程序的整个生命周期中,因此我不希望在BaseViewController上调用dealloc。

Sorry, if this sounds confusing, but essentially, the flow looks like this: 对不起,如果这听起来很混乱,但基本上,流程看起来像这样:

  1. AppDelegate sets rootViewController to BaseWebViewController AppDelegate将rootViewController设置为BaseWebViewController
  2. BaseWebViewController's viewDidLoad also calls the superclass BaseViewController's viewDidLoad BaseWebViewController的viewDidLoad也调用了超类BaseViewController的viewDidLoad
  3. Majority of the lifetime of the app is spent in BaseWebViewController, but occassionally SecondaryWebViewController needs to be pushed 应用程序的大部分生命周期都花在了BaseWebViewController上,但偶尔需要推送SecondaryWebViewController
  4. SecondaryWebViewController's viewDidLoad and dealloc calls the superclass's viewDidLoad and dealloc causing strange behaviour. SecondaryWebViewController的viewDidLoad和dealloc调用超类的viewDidLoad和dealloc导致奇怪的行为。

Currently, if I comment out [super viewDidLoad] and [super dealloc] in SecondaryWebViewController so that BaseViewController is never dealloced, then everything runs fine. 目前,如果我在SecondaryWebViewController中注释掉[super viewDidLoad]和[super dealloc],以便从不释放BaseViewController,那么一切运行正常。 However, I don't know if this is the right way of doing things. 但是,我不知道这是否是正确的做事方式。

What is the approach of calling super functions in a subclass such as this scenario? 在子场中调用超级函数的方法是什么?

If I understood you correctly you are mixing the concepts of Classes and Objects. 如果我理解正确的话,你就是在混合类和对象的概念。

By your description, you have two view controllers objects in your app. 根据您的描述,您的应用程序中有两个视图控制器对象 One of them is an instance of the BaseWebViewController class and the other is an instance of the SecondaryWebViewController class. 其中之一是实例 BaseWebViewController类和另一种是实例 SecondaryWebViewController类。

These two instances are completely separate from each other. 这两个实例彼此完全分开。

When your instance of SecondaryWebViewController calls dealloc , when it calls [super dealloc] this just invokes the dealloc method of the BaseWebViewController class - On the same instance. 当您的SecondaryWebViewController 实例调用dealloc ,当它调用[super dealloc]时,它只调用BaseWebViewController类的dealloc方法 - 在同一个实例上。 The other view controller instance is completely unaffected. 另一个视图控制器实例完全不受影响。

I might have completely misunderstood, and in that case please excuse me. 我可能完全被误解了,在那种情况下请原谅我。 If I did understand correctly, you should look up the concepts of classes and objects, this is really important basis for going on further. 如果我理解正确,你应该查找类和对象的概念,这是进一步深入的重要基础。

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

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