What does UIViewController class do in viewDidLoad? [duplicate] - iphone

This question already has answers here:
`[super viewDidLoad]` convention
(2 answers)
Closed 9 years ago.
As in the title, I was wondering what is the "default" implementation of viewDidLoad in UIViewController? Does it really do anything? Also does it matter if in my UIViewController's subclass I write
-(void)viewDidLoad{
[super viewDidLoad];
/*custom code here*/
}
or
-(void)viewDidLoad{
/*custom code here*/
[super viewDidLoad];
}
?
P.S. This is not a duplicate, in other questions people ask when should they call [super viewDidLoad], while my main concern is what the UIViewController's implementation do with it.

That implementation does nothing, and can safely be removed if you have no setup to do after the view loads. However, it is fairly rare to have no custom setup to do here; this is the place where your view controller is telling you that all of its UI objects are available to customize with data. It's included in the template with an empty implementation as a reminder: here's where to do this.
As far as when to call super: the general expectation is that setup or initialization methods call super before doing work, and teardown methods call super after doing work.

ViewDidLoad Method Called after the controller’s view is loaded into memory.
This is where you want to instantiate any instance variables and build any views that live for the entire lifecycle of this view controller. However, the view is usually not yet visible at this point
This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.
For detail Information

Related

iOS: How to know if viewDidLoad got called?

Is there a BOOL or some other way of knowing if viewDidLoad: has been called?
I need to change a view property every time my view has entered active, but if the view hasn't ever been loaded, I don't want to prematurely trigger viewDidLoad:. If there isn't way of easily telling if viewDidLoad: has been called, I'll simply add a BOOL called loaded set to NO in the view controller init, then change the view properties after entered active if loaded is YES or in viewWillAppear: if loaded is NO then set loaded to YES.
Use isViewLoaded. Other than that it does exactly what you want, there's not that much to say about it. The documentation is as simple as:
Calling this method reports whether the view is loaded. Unlike the
view property, it does not attempt to load the view if it is not
already in memory.
Perhaps you should init your UIView in viewDidLoad, and then change it in whichever way you need to inside viewWillLayoutSubviews.
Here's the pedantic answer to this question. If you want to know when viewDidLoad has been triggered, you have to implement viewDidLoad in your view controller
- (void)viewDidLoad
{
[super viewDidLoad];
viewDidLoadCalled = YES; // Not actually the best way to do this...
// Set up more view properties
}
But as Tommy says, you actually need to use isViewLoaded. This gets around the problem of doing a check like
if (!self.view) {
// do something
}
which inadvertently loads the view by virtue of asking about it.
Be aware that by the time viewWillAppear: is called, the view will always have loaded. Also, on older (pre-iOS 6 I think) releases, the view can unload and be reloaded many times over a view controller's lifetime. Refer to the very nice Big Nerd Ranch view lifecycle diagram for the old behavior. It's almost the same in iOS 6+, except that the view doesn't unload under low memory conditions and viewDidUnload doesn't get called:

InitWithNibName and viewDidLoad? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
initWithNibName VS viewDidLoad
I'm new at iOS development. I would just like to know the main differences between these 2 functions ?
Thanks for your help
viewDidLoad
Is called when the view loads and is initiated/Unarchived and loaded into the memory. This is a great customisation stop.
initWithNibName:
Is used for initializing a certain class ( it is an overriden init method) with a xib file's name, the bundle parameter specifies the location of the file, you would pass nil for the main bundle, which is the projects folder.
You should set up your properties in the viewDidLoad. The initWithNibName: is something that you call when you create a controller instance from a nib File. I wouldn't put customisation code there.
viewDidLoad:
viewDidLoad is a part of iPhone application life cycle.This method is called after the .xib file is read and the outlets and actions are connected to your view controller. In this Method you can do assignment with your outlets. This method get called as many times as your view get Loaded and unloaded.
initWithNibName:
This method is the designated initializer for UIViewController classes. It's used whenever you're creating a UIViewController object in the code. This method sets up UIViewController to be able to load a nib on demand. This method is helpful when you are performing something different logic in init method.

How to add the UI for one of the tab controller's using loadView when the rest were via Interface Builder?

I have a main window which has the UITabViewController as its root controller. I am using a nib file for this. In Interface Builder, two of the tabs have been wired to Controller_A, Nib_A and Controller_B, Nib_B but the 3rd tab only knows about Controller_C.
I assumed that this would mean that the loadView method of Controller_C would be automatically called since I haven't bothered to specify the NIB file. I want to lay this piece out programmatically. And it DOES indeed get called as I've confirmed by placing a breakpoint inside this method.
BUT when I switched over to Controller_C in the simulator, it comes up empty!
Here's what the loadView of Controller_C looks like:
- (void)loadView
{
[super loadView];
...
[self setTableView:formTableView];
[view addSubview:formTableView];
[self setView:view];
}
Any tips? What am I ignoring?
Looks like you are searching for:
-(void)viewDidLoad {
Here are the main things to check.
Click on your Controller_C in interface builder. In the Identity Inspector, make sure that the Class field is set to Controller_C.
In the Attributes Inspector, make sure the NIB Name field is blank.
If you have an existing Controller_C.xib laying around in your project, remove and delete it. The default implementation of loadView loads this file even if
Remove [super loadView]. Since you're building your view hierarchy in code, you shouldn't invoke the default implementation. You should explicitly allocate the controller's view as a local variable in loadView and set it using setView:.
Also, your comment on the other answer suggests that you may be confused about when/why loadView and initWithNibName:bundle: get called, so let me clarify:
loadView gets called to lazy load your controller's view the first time its view property gets accessed. This is true whether your view controller was constructed as an object in a NIB, or whether you constructed it yourself in code using initWithNibName:bundle:. The default implementation of loadView loads the NIB that was specified in initWithNibName:bundle: or in the NIB Name property in IB. If a NIB name wasn't specified, the default implementation looks for any NIB in the bundle that has the same name as your class and loads that NIB if one is found. If no appropriate NIB is found, then the default implementation of loadView just creates an empty view and sets that as your controller's view. When we build our own view hierarchy explicitly in loadView, we don't want any of these default behaviors, so we don't call [super loadView].
It seems loadView works as expected, the reason that my view was blank is because the datasource for my tableview was actually NIL. Earlier, I did not consider this scenario as I thought that it would at least present an empty table in such a case but apparently that was an incorrect assumption on my part. All this mistakenly led me to believe that the view wasn't being initialized properly.
#cduhn: Thanks, I had been following steps 1-3 already and it was really good to hear someone else give the same advice. The rest of what you said was educational for me as well.
Thanks Everyone.

UITableViewController loading inside a UIViewController inside a UIViewController issue

I don't really know if what I'm doing is the right way to do it. Right now it seems to be working until it hits a certain point with a EXC_BAD_ACCESS message.
I'll describe what I'm doing as best and with the most relevant details I can tell:
I have a CalendarViewController that inherits UIViewController which is loading from a .xib file (CalendarViewController.xib). The class contains a UIView called contentView which I created and which I initialize with another nib file based on a class which is also inherited from UIViewController:
- (void)viewDidLoad {
[super viewDidLoad];
calendarView = [[CalendarView alloc] initWithNibName:#"CalendarView" bundle:nil];
[contentView addSubview:calendarView.view];
}
(calendarView is the class inheriting UIViewController and viewDidLoad is from CalendarViewController.
CalendarView.xib has a UITableViewController with it's respective UITableView. This Table View Controller is linked to a CalendarTableController to which I also generated a .xib file for it.
Everything is being created just right (apparently) but it is crashing somewhere very unexpected. CalendarTableController also implements a DateLoaderDelegate which loads information from an xml on an external url.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// When the data has all finished loading, we set a copy of the
// loaded data for us to access. This will allow us to not worry about
// whether a load is already in progress when accessing the data.
self.lastLoadedMatchXMLData = [self.matchXMLData copy];
// Make sure the _delegate object actually has the xmlDidFinishLoading
// method, and if it does, call it to notify the delegate that the
// data has finished loading.
if ([_delegate respondsToSelector:#selector(xmlDidFinishLoading)])
{
[_delegate xmlDidFinishLoading];
}
}
The application is getting to this point without any problem. _delegate is containing the correct object (a CalendarTableController which implements the DateLoaderDelegate). But when it arrives to the line:
if ([_delegate respondsToSelector:#selector(xmlDidFinishLoading)])
it crashes with the EXC_BAD_ACCESS, I don't really know the reason, if I look at the debugger, the crash is not occurring in any of my classes as any of them are appearing in the execution stack. URLConnectionClient seems to be generating it, but I don't really know why. The loading of the xml had worked earlier before I rearranged the ViewControllers to work as I have described now.
Any ideas?
It's weird. I fixed the problem but I had to dedicate the whole UIViewController to contain the UITableView. What I did was this:
Create an IBOutlet with the custom UITableViewController (CalendarTableViewController) in the custom UIViewController.
In the loaded .xib file I linked the IBOutlet to a created UITableViewController declared as a CalendarTableViewController.
This way I solved the problem of the UITableViewController being deallocated without reason. But now the image views I had placed in the intermediate UIViewController wouldn't appear. I had to set that UIViewController to contain solely the CalendarTableView and place the image views in the initial UIViewController. Weird, isn't it? I don't like much the hierarchy I created... but for now that will do =S
Check that you have defined properties for all of your subviews and that you are retaining everything that you need to be. Bad Access May mean that you're attempting to call respondsToSelector on an object that has been released.
Have you tried calling loadView before adding the nested controller's view to the parent's view?
Maybe viewDidLoad is not executing before you add the view and some variables were never initialized.

viewDidLoad(), LoadView() [duplicate]

This question already has answers here:
iPhone SDK: what is the difference between loadView and viewDidLoad?
(8 answers)
Closed 9 years ago.
What is the difference between viewDidLoad() and LoadView()? In what way are they different from each other?
Which one is better when we develop applications without using XIB ?
Thanks .
ViewDidLoad is called when your view loading is finished and loadView is called when loading starts.
And when you make a new project you see comments on these methods which clearly gives a tip when you should use which function
see this
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
These comments are clear and easy to understand.
viewDidLoad()
is to be used when you load your view from a NIB and want to perform any customization after launch.
LoadView()
is to be used when you want to create your view programmatically (without the use of Interface Builder).
If you intend to use IB to build your UI, you should do all your post IB initialization in viewDidLoad. The class will not call loadView at all if you use a nib to initialize a controller.
If you initialize the controller in code, the viewController will call loadView first, then viewDidLoad. You can do all your initialization in loadView, or viewDidLoad, depending on your preferences.
However, if you decide to use loadView, be sure to set the view property before attempting to read self.view, otherwise you will enter into an infinite loop and crash.
If you initialize your view from stroyboard or xib file, don't override this method or call [super loadView] inside.
if you call [super loadView] inside the method, you better never override this method and put the following code to your viewDidLoad method.
if you initialize your view programmatically, you should NEVER call [super loadView]. and You must assign your rootView to self.view property, or you may get a perfect crash.
Isn't it obvious?
viewDidLoad is called... When the view finishes loading.
loadView is called when the view is told to load.
Neither is better or worse. It's all dependent on your design.
Good luck :)
view controller loads its view from nib associated with it if there is no nib associated, then it automatically called it's loadView() method to fill it's View.
In that case you need to implement loadView() method.
by default it returns nil
when your view loads in to the memory viewDidLoad() method is called here you can do your custom initialization according to your requirement.
If you are developing applications without using xib LoadView() method is called and if there is an xib then ViewDidLoad method is called
So it is better to use LoadView method.