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.
Related
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 ?
I'm a little bit lost. I have created a small app that is starting with a tab bar and in one of its view there's a button that should open a Navigation view that contains a table view.
In my NIB file I have put a Navigation Controller that contains a TableView Controller. I have created a sub-class called MyTableViewController which inherits from the UITableViewController. In the NIB I have configured the Custom Class of the TableViewController with my subclass MyTableViewController.
When the button of my App is tapped, I'm loading the NIB file with the initWithNibName but it returns me a UINavigationController.
How does it work to request the creation of MyTableViewController and get a pointer on it when I'm loading my NIB?
Thanks,
Sébastien.
This one has caught me out a few times.
When you do initWithNibName it will take the class from the custom class of the File's Owner, not the custom view of any objects .
I dont actually bother subclassing from UITableviewController any more. Just create a view controller and drag in a table view as a subview. Just make sure you hook up the data source and the delegate.
Link your TableViewController to an IBOutlet so you don't have to mess with initWithNibName.
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...?
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.
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.