Not calling [super viewDidLoad] and [super dealloc] from a subclass - iphone

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. In BaseWebViewController, I am calling [super viewDidLoad] and [super dealloc] in their respective methods.
Throughout most of the app, the user will be navigating through the UIWebView found on BaseWebViewController. But on certain links, I need to push (and later, pop out of) 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.
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]. Since BaseWebViewController must always be present throughout the entire lifetime of the app, I don't ever want dealloc called on the BaseViewController.
Sorry, if this sounds confusing, but essentially, the flow looks like this:
AppDelegate sets rootViewController to BaseWebViewController
BaseWebViewController's viewDidLoad also calls the superclass BaseViewController's
viewDidLoad
Majority of the lifetime of the app is spent in
BaseWebViewController, but occassionally SecondaryWebViewController
needs to be pushed
SecondaryWebViewController's viewDidLoad and dealloc calls the superclass's viewDidLoad and dealloc causing strange behaviour.
Currently, if I comment out [super viewDidLoad] and [super dealloc] in SecondaryWebViewController so that BaseViewController is never dealloced, then everything runs fine. 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.
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. 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.

Related

ViewController subclasses should have [super viewDidUnload] in each?

I have subclassed UIViewController 3 times. Should each one have [super viewDidUnload] in it?
Hopefully a simple question, and not too daft! Thanks.
- (void)viewDidUnload
{
...
[super viewDidUnload]; //<<< In all 3 subclasses?
}
Yes, you should call super in each class.
Note that this method is deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.
You should call [super viewDidUnload] in viewDidUnload method call.
You should read the ios dev document (memory management subsection). I am sure it will help you to understand the answer of your question ;)

Iphone dev - ViewDidUnload

I'm using ARC
In some tutorials I'm watching, he once made a UILabel and then in the method ViewDidUnload he wrote: self.theLabelVariable = nil;
I have not seen him write anything in ViewDidUnload again since then.. yet.
I'm a bit confused to what code is supposed to go in that method.
1.
What and when should I write in it?
It's commented:
"Release any retained subviews of the main view. E.g. self.myOutlet = nil;"
Not really sure what that's upposed to mean though
2.
It also has a "[super ViewDidUnload]" What does that do?
3.
Someone said "etting it to nil when you don't intend to use it again is a good idea".
But in the tutorial where someone wrote "self.TheLabelVariable = nil", when I hit the home button and then brought the app up again, everything worked fine.
Or is it not cViewDidUnload when you hit the home button?
I suppose the third question is: When is it called?
You should do this with all of your IBOutlets. You should set them all to nil. And [super viewDidUnload]; is unloading the superview! Its sort of like [super viewDidLoad];
1 - You should use viewDidUnload to release (set to nil if it's a property) any object that is not needed when the view is unloaded, like views, and that you'll recreate again on viewDidLoad. Example: You don't need a label when the view is unloaded (by pressing the home button), then you release it, and then you create it again when the view loads (app come up).
2 - [super something] calls the method on the super class. For example, if you have a class that is a descendant of UIViewController calling [super viewDidUnload] will execute the Apple's UIViewController viewDidUnload code (which might be empty... or not).
3 - See the example on 1. But the thing is: when you don't need an object anymore set it to nil so it can be released and you're not wasting memory.
Let me know if I missed something or if there's something you didn't understand.
EDIT
For views that are initialized from a XIB file (interface builder), called IBOutlets, you don't need to do self.label=nil; because UIViewController does it for you. Just like it initialized it for you, it will release it too.

When overriding initWithCoder is it always necessary to call [super initWithCoder: coder]

In this code I am loading a View Controller (and associated View) from a .xib:
-(id)initWithCoder:(NSCoder *)coder
{
// add custom initialisation code here
[super initWithCoder:coder];
return self;
}
This successfully works, but I do not really understand what the line [super initWithCoder:coder] is accomplishing. Is that initializing my View Controller after my View has been initialized?
Please be as explicit as possible when explaining. Thanks.
Your class is a subclass of UIViewController. The call is telling your super class (UIViewController) to do the steps it needs to accomplish so that you can do your init steps. This would be setting up any properties that the UIViewController provides or registering for notifications that the UIViewController needs to do its work.
It is suggested almost every time you override a method from the super class to call the super class's method in addition to the steps you need to take.
Edit: Also if you don't need to do anything in a method the superclass provides, you can just leave it out and the super class's method will be used instead. In this case I would not provide the initWithCoder: method unless there was some code you need to preform in addition to what you showed.

Subclassing NSObject, can it cause problems?

I have a very basic data class that is subclassed from NSObject. I declare a few strings, make sure they have properties (nonatomic, copy), and synthesize them. The only method I implemented was dealloc() which releases my strings. Can any memory problems arise from just this? Are there any other methods I need to implement?
Subclassing NSObject is something that we do all the time. Just follow the memory management rules, and you're good to go.
You could implement a custom init if you want to set anything up.
-(id)init {
if (!(self = [super init]))
return nil;
// Set things up you might need setting up.
return self;
}
But that's only if there's something you want to have ready before you call anything else on the class.
Just having a dealloc method should be fine, otherwise.
There won't be any problems. Subclassing NSObject is perfectly accepted, and in 99% of cases required.
By subclassing NSObject, your subclass receives all the required behaviour that is expected of any object in Cocoa/Cocoa Touch. This includes things like the reference counting memory management system using retain and release etc.
What you're doing is fine. Be sure to call [super dealloc] at the end of your subclass' -dealloc method.

UIViewController loadView method

when overriding the loadView method in UIViewController, should one call [super loadView] in the beginning of the method or at the end of the method? And why?
According to the UIViewController class reference, you should not call [super loadView] at all:
Your custom implementation of this method should not call super.
Normally you should not call loadView directly. It merely sets your self.view property and is called by the view controller only.
You should call [super loadView] only if you need the view created by your super class, because you want to include it in your decoration view hierarchy or something like that.
Just to be really sure, you didn't mean viewDidLoad, right? Because they are two very different methods... as of 3.0, the docs reccomend always calling viewDidLoad at the start.
You can call it either before or after, but usually it is placed at the end unless you have a reason to do otherwise.