what to call viewDidLoad or loadview - iphone

I have a view controller class which loads from a .nib file. However, I also want to add controls (like UISwitch) to that view programmatically (UISwitch is not added to the nib file). In which portion of my code should I allocate the UISwitch control, viewDidLoad or loadView method?

I would do it on viewDidLoad. Definitely.
From Apple's documentation:
Discussion
This method is only invoked
when the view property is nil and it
is needed for display. You should not
invoke this method directly.
If you create the view that this view
controller manages programmatically,
then you should override this method
to create your view. The default
implementation creates a UIView object
with no subviews.
However, if you initialize the view
using a nib file—that is, you set
thenibName and nibBundle
properties—then you should not
override this method because the
default implementation already reloads
the nib file. Instead override the
viewDidLoad method to set any
properties after the nib file is
loaded.
In your case, the UIView is being created from the NIB file.

Use viewDidLoad. Additionally you should remove everything you added in the viewDidUnload method.

If you are loading from a NIB, implementing loadView will cause an error. Use viewDidLoad. As Pablo says, this is well documented by Apple.

Related

Access UI components Inside controllers init method

I'm trying to hide a button inside the controller's init method. But its not working. I have created a outlet to the image to access it from the controller. Dont know why the image is not get hidden.But I can do it inside the viewdidload method. Is it because the view still not loaded?
Yes the view will be loaded when viewdidload is called. so any changes to the existing UI should be called in view did load or view will appear
The UI components you're talking about are all subviews of the UIView that is owned by the UIViewController that you're creating. But that UIView and its subviews aren't actually created at the time the init method is called, so you can't change them there.
viewDidLoad is the appropriate place for any code that only needs to run once after the view has been created, but before it has been displayed.
viewWillAppear: and viewDidAppear: will run every time the view is about to or just did become visible again respectively.
Actually, the whole view hierarchy will be loaded when the loadView message is sent to the controller. This message is also sent automatically the first time you access the controller's view. That's why your message to the button gets discarded in the init method, because the object is not yet 'unfrozen' from the xib/storyboard. And yes, the appropriate place to do your view customization would be inside the viewDidLoad delegate method.
PS. Also... if you want the button to be hidden upon initialization, why don't you just check its hidden property in your xib/storyboard ?

Best place to add a navigation bar button?

Where should I place my code that creates a UIBarButtonItem and assigns it to self.navigationItem.rightBarButtonItem? In initWithNibName or viewDidLoad?
EDIT: I should clarify that I'm loading a NIB from Interface Builder.
viewDidLoad is the correct place to create the button programatically whether you are loading a NIB from Interface Builder or not.
From the UIViewController class, viewDidLoad method documentation
This method is called after the view controller has loaded its
associated views into memory. This method is called regardless of
whether the views were stored in a nib file or created
programmatically in the loadView method. This method is most commonly
used to perform additional initialization steps on views that are
loaded from nib files.
In - (void) loadView if you do it pragmatically. Otherwise do as Rog suggests.

When should I set a custom class override default UIViewController for a xib file?

When should I set a custom class to override default UIViewController for a xib file?
For example, I have a UIViewController subclass named SettingViewController, and a xib file namedSettingViewDetail.xib.
I found whether I set a custom class for the xib or not(default is UIViewController), my following code will work normal, it will create a controller for me
SettingViewController *oneView = [[SettingViewController alloc] initWithNibName: #"SettingViewDetail" bundle:nil];
and I can use it to control view navigation:
[[[[UIApplication sharedApplication] delegate ] naviController] pushViewController: oneView animated:TRUE];
I want to know when should I set a custom class for xib, and what reason for this?
If you want to set outlets (references to your subviews in this .xib) and have simple (without using tags) access to that subviews in your SettingViewController then you should set appropriate class of File's Owner and then that IBOutlet's will appear.
In similar way you can easily set methods (IBAction's) that will be called when user click the button, for example.
So, in the general case, you can just reduce amount of your code.
As far as the xib file goes, I think you are talking about the class identifier for the file's owner? That only needs to be your custom subclass if you have specific actions or outlets that you are connecting in interface builder that only exist in your subclass.
If there are no subclass specific features you are using in the xib, the you can use UIViewController instead. This would allow you to reuse the same xib file for several different view controller subclasses if you liked.

viewDidLoad not called for UITableView loaded from XIB file

I have a XIB file in which I have a few UI elements. The resulting view is controlled by my own class derived from UIViewController. This one works fine, and viewDidLoad is called, when the XIB file gets loaded etc from the main application delegate.
However, in that XIB file I also have (obviously) some sub views. One of them is a UITableView. That one is being controlled by a custom class of mine derived from UITableViewController. I've set the delegate, datasource outlets on the one side, the UITableViewController also has got the correct view property set. However, the viewDidLoad of the UITableViewController is NOT called when the surrounding view loads.
I think this because the main UIView / UIViewController pair does not really know about the subview. in the UIViewController viewDidLoad I can call the subview's viewDidLoad. However I am suspecting that this is not the intended usage. So what should I do instead?
What I was assuming was, that subviews loaded from a xib file all get the viewDidLoad message.
You're correct in that you shouldn't call the subview's viewDidLoad: method, as it should always get called when the view is loaded (regardless of how the view is created). From the documentation:
This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method.
This suggests to me that something (connections?) haven't been set up correctly in your .xib file, or that the controllers haven't been properly linked with their views. When you say that the UITableViewController has the correct view set, are you using the "view" property instead of the "tableView" property of the UITableViewController...?

Help on creating a Custom View

I am planning to write a custom view that will render some stuff using Quartz.
After looking into some sample code in apple site, I decided to subclassing UIView to create my own view:
#interface myView : UIView {
}
#end
Now my question is, what would be the best way t bind my view with the viewController?
While loading from NIB file, we directly assign the view controller file as fileowner type.
In case of custom UIView, how can we do this?
When not working with nib files, you use the loadView method in a UIViewController to create your view. This comment even appears above loadView in the template for a new UIViewController:
// Implement loadView to create a view hierarchy programmatically, without using a nib.
Edit: I may have been too terse here. Basically, then inside your loadView method, you instantiate your myView (using alloc/init, or whatever), and assign it to self.view. Let me know if you need example code.
A little confused by your wording. I assume you want to place your custom view in your UIViewController.
You can easily do this both in Interface builder and within your code.
Code:
myView *view = [[myView alloc] initWithRect:CGRectMake([x],[y],[width], [height]];
[self.view addSubView:view];
[view release];
In Interface Builder:
Select "View" in the Library Objects.
Drag View unto your UIViewController's view.
In the inspector click the i in a circle icon ("View Identity")
Using the class dropdown select the class of your view (myView)
Here's more info: http://developer.apple.com/iphone/library/documentation/DeveloperTools/Conceptual/IB_UserGuide/CustomizingInterfaceBuilder/CustomizingInterfaceBuilder.html#//apple_ref/doc/uid/TP40005344-CH16-SW12
If your viewController already has an Interface Builder .nib file (as when built with the Xcode templates), open it. There's usually a view already in it. You can bring up the inspector for that view, and in the Identity tab of the inspector, change the class of the view controller's view from UIView to your custom view class. Save, rebuild, and your custom class is now bound and will load with that view controller.
If you write and compile your custom view controller (it can be an empty template), it should appear in the inspector to just select as the class for the view controller's view.