awakeFromNib is not calling but displaying xib - iphone

I have created a new project in xcode 4.5 and I have called my viewcontroller by
-(id)initWithNibName:bundle:
from appdelegate as it called in default project template and i am not using storyboard
but awakeFromNib was not called
I have also searched but not able to understand why awakeFromNib is not calling.
By apple documentation for -(void)awakeFromNib
"Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file."
"Objects that conform to the NSCoding protocol (including all subclasses of UIView and UIViewController) are initialized
using their initWithCoder: method. All objects that do not conform
to the NSCoding protocol are initialized using their init method. After all objects have been instantiated and
initialized, the nib-loading code reestablishes the outlet and action connections for all of those objects. It then
calls the awakeFromNib method of the objects."
My question is that Why awakeFromNib is not calling when xib file is loaded?
Is i am doing something wrong?Please elaborate

awakeFromNib: is called only if the view controller is inside the nib file.
In this case you init the view controller with init initWithNibName:bundle, so the content of the view controller is inside the nib, not the view controller itself.
If you open the xib file, drag an object inside the xib and set the class to be your view controller class name, then awakeFromNib will be called (this case you don't need to initialize manually the view controller).
So keep in mind the difference between having a view controller in a xib file, and having a view controller not in a xib file, but with all it's contents loaded from a xib file.
PS: I used nib and xib interchangeably.
EDIT
I forgot to tell you how to go around this.Override viewDidLoad to do things with the initialized outlets.

Why not just add the -(id)initWithNibName:bundle: method to your subclass instead of -(void)awakeFromNib?
My "awakeFromNib" methods are called just fine but I load my view controller with:
myViewController *vc = [[[NSBundle mainBundle] loadNibNamed:#"myViewController" owner:self options:nil] objectAtIndex:0];
so I'm wondering if your awakeFromNib isn't called because you're using initWithNibName instead?
EDIT: I just saw your comment that you're adding the awakeFromNib to the ViewController of the default project that is created by XCode. Did you uncheck the "use StoryBoard" box when you created your project? If your nib is inside of the project's main storyboard, there may be some extra hoops you have to jump through.

Related

I can make UINavigationController load only at 2nd level, not at Root View Controller

I tried looking for a similar problem but I could not find a similar question.
I am loading a UINavigationController in a UIView which is not (as in most examples) the MainWindow.
I created one new .xib called DocumentsViewController which is subclass of UIView (it has the related .m and .h files). And I created a DocumentsRootViewController.xib, which is a subclass of UITableViewController, which is supposed to be the UINavigationController's RootViewController.
I moved to DocumentsViewController and added a UINavigationController object in Interface Builder. Then I went to code, and added it as in IBOutlet, and connected it to the object.
In the ViewDidLoad, I execute the following lines:
DocumentsRootViewController *rootViewController = [[[DocumentsRootViewController alloc] init] autorelease];
rootViewController.title = #"Documents";
[navigationControllerDocuments initWithRootViewController:rootViewController];
[self.view addSubview:navigationControllerDocuments.view];
It shows the table as intended, but it shows a "Back" button to the "Root View Controller" (as in picture below).
Why? Shouldn't it already know that the rootviewcontroller has been set?
Thank you in advance to the ones that clarify this doubt
Giovanni
When you add the UINavigationController via the Nib it actually creates an instance of a UINavigationController inside the nib file with a default RootViewController set (of type UIViewController) and with a default title of RootViewController.
When you load the nib, this object is being created as part of loading the nib (i.e when you initialise DocumentsViewController) - so the navigationControllerDocuments outlet is already initialised as a UINavigationController with the default ViewController already set on it.
What I think is happening is when you call 'initWithRootViewController' - you are calling this on an already initialised object - so it is running the initialisation code again - pushing the second view controller (the DocumentRootViewController) onto the stack, but the default one that was created in the nib is already there.
What you should probably do is forget about creating one in the nib and initialise the whole thing programatically.
i.e. where you do:
[navigationControllerDocuments initWithRootViewController:rootViewController];
I suggest that you do an alloc and init instead:
[[navigationControllerDocuments alloc] initWithRootViewController:rootViewController];
Since you are doing this you really don't need to have the navigation controller added to the nib so if this works you should remove it from the nib since you are replacing it with this one in code.

Put a UIView into a UITableView Header

I have a UITableView which is in my xib file. And I created a property like this for the controller:
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
Now, I want to have a table view header (not a header for each section). So, because I want to have custom styling, I created a new xib file with a view (and I connected to my controller which has the myTableView implemented).
Then I can write in viewDidLoad in my controller:
[self.myTableView setTableHeaderView:self.myTableHeaderView];
where myTableViewHeader is a UIView property in the controller.
Unfortunately, the UITableView won't display this UIView, so my question is, how can I put a UIView to a UITableView into the header?
Thank you in advance & Best Regards.
A couple of things to check:
myTableHeaderView must also be an IBOutlet or created in code in your Class somewhere
self.myTableHeaderView must not be nil when you're trying to add it as the table header view. If it is nil then you didn't hook up your outlets correctly
if you've designed the table view header in IB in its own .xib file, then you must somewhere call this (viewDidLoad is a good place):
[[NSBundle mainBundle] loadNibNamed:#"MyTableViewHeader" owner:self options:nil];
The File's Owner of the MyTableViewHeader.xib must be your TableViewController subclass, and you must hook up the myTableHeaderView object to File's Owner's corresponding outlet.
EDIT: in answer to "what does [[NSBundle mainBundle] loadNibNamed:#"MyTableViewHeader" owner:self options:nil];" do?
This one little line contains the magic that will open your eyes to how XIB files and Objective-C objects work together in Cocoa (touch), elevating your iOS programming kung-fu to entirely new levels. There are two classes of Cocoa programmers, those who understand what it does and use and benefit from it, and those who don't yet know what they're missing, and instead stumble through the wilderness trying to create XIB files for their objects and never quite getting it to work.
With that massive build-up here's the details:
A XIB (NIB) file is a collection of archived Objective-C objects (and reference to objects not actually within the XIB, like "File's Owner", a so-called "proxy object") and connections between these objects. When a XIB file is loaded, these archived objects are brought to life in exactly the state they were saved into the XIB, and then the connections between those live objects (and "proxy objects") are made according to the connections recorded in the XIB file.
For example, in your standard UIViewController subclass .xib file, you have File's Owner set to your MyViewController class. Inside the .xib is a UIView object, which usually itself contains other UIKit objects. The "view" outlet of the UIViewController class is set to point to the UIView object in the .xib. When this .xib file is loaded, the UIView object is unarchived and becomes a living UIView object, with all of the properties and settings recorded in the .xib. That's the "unarchiving part".
And then, because of the connection in the .xib from File's Owner (MyViewController class) to the UIView object, the pointer to this new UIView object is stored in the "view" field of your MyViewController instance. Any other connections also present in the .xib file, like UILabels, UIButton actions, etc., are also set up, to any other "IBOutlet" fields in MyViewController.
This all seems like magic and happens with the
(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
method when you init a new UIViewController subclass.
Now the good part: you get to do this sort of associating nib files with objects yourself! You can use the loadNibNamed:owner:options method to associate any .xib file with matching set of nil IBOutlets in any object that you want!!!
All of a sudden, creating entirely custom table view cells, table headers, footers, whatever, is a breeze, you can easily write modular reusable UIView components, etc., all laid out in Interface Builder.
The object whose nil IBOutlets you want to fill in with objects loaded from a .xib file is the "owner" object. Usually (but I'm not sure this is absolutely required, any class with the identically typed and named IBOutlets set to File's Owner may work), this is the class that will be specified as "File's Owner" in the xib.
OK, now you've got your existing owner object with nil IBOutlets (the IBOutlets must be nil or they won't be changed, that's the "rule" of loading XIBs into an owner object. It's ok that some IBOutlets are not nil, they just won't be changed when you load the XIB file, and usually that's what you want), and you've got your .xib file with objects that you want to load into the owner object. You call:
[[NSBundle mainBundle] loadNibNamed:#"MyXIBFileToLoad" owner:theOwner options:nil];
.. and voila! Now any nil IBOutlets in "theOwner" that are connected to objects in the MyXIBFileToLoad.xib have been set to the new objects loaded from the XIB file.
(also, this method returns an array of all objects unarchived from the xib. If you don't care about setting any owner's outlets, you can just search this list for your objects by class and by tag).
So that's the story, now go crazy with new ways to associate Objective-C classes with objects stored in XIB files!

How to use generic (NSObject) controller with subviews of a UIViewController?

I have a UIViewController that is loading several subviews at different times based on user interaction. I originally built all of these subviews in code, with no nib files. Now I am moving to nib files with custom UIView subclasses.
Some of these subviews display static data, and I am using loadNibNamed:owner:options: to load them into the view controller. Others contain controls that I need to access.
I (sort of) understand the reasons Apple says to use one view controller per screen of content, using generic controller objects (NSObjects) to manage subsections of a screen.
So I need a view controller, a generic controller, a view class and a nib. How do I put this all together?
My working assumptions and subsequent questions:
I will associate the view class with
the nib in the 'class identity' drop
down in IB.
The view controller will coordinate
overall screen interactions. When
necessary, it will create an instance
of the generic controller.
Does the generic controller load the
nib? How?
Do I define the outlets and actions
in that view class, or should they be
in the generic controller?
How do I pass messages between the
view controller and the generic
controller?
If anyone can point me to some sample code using a controller in this way, it will go a long way to helping me understand. None of the books or stackoverflow posts I've read have quite hit the spot yet.
Okay, I think I figured it out:
Extend NSObject to make your CustomController
Define your outlets & actons in CustomController.h, including a reference to the UIView in your nib
Set the File's Owner of your nib to CustomController
Hook up all your outlets & actions as usual, including the UIView outlet
In your CustomController.m init, load the nib
- (id)init {
self = [super init];
if (self != nil)
[self loadNib];
return self;
}
- (BOOL)loadNib {
NSArray *topLevelObjs = nil;
topLevelObjs = [[NSBundle mainBundle] loadNibNamed:#"CustomView" owner:self options:nil];
if (topLevelObjs == nil) {
NSLog(#"Error! Could not load nib file.\n");
return NO;
}
return YES;
}
The new NSObject based controller will work very much like a view controller.
It sounds like what you want is what I've coined "reusable UIView widgets" -- reusable widgets that do something / present a display that you can incorporate in your app screens wherever and how many times as you want -- you can create them purely in code or instantiate them by placing their frame in another xib file (but you don't get to change the widgets' internal parameters in the xib file, that would require an IB plugin).
This is an organization that I've never seen discussed anywhere. Part of my frustration early on with iOS programming is wanting something like this but not seeing any way to express it in any example.
See my answer in this question to see how it can be structured:
UIView and initWithFrame and a NIB file. How can i get the NIB file loaded?
I recommend placing all widget-internal handling of direct/low-level events in the uiview widget subclass, and implememnt a delegate protocol for a highler level interaction with the widget client (i.e., "loginRequested:userName:passWord" instead of manually accessing button and textfields internal to the widget).
The (optional but recommended) xib file for the widget has an owner of the widget, and the init code in the widget owns the duty of loading the xib file. The customer of the widget simply instantiates the widget and implements whicever widget delegate functions makes sense for it.

Call a function in a UIViewController's UIView

I've created a view controller and the associated view using the interface builder. I'm trying to call a function that I added to the UIView from the UIViewController. I'm not sure how to call that function though.
I've tried
[self.view myFunction]
but that just makes my program crash.
Did you declare your IB outlets and connect the method to it?
Your view (more accurately nib)'s file owner should be set to your viewController class.
Unless your calling drawing functions and methods you shouldn't call anything on the view. You just don't need to.
Edit: grammar corrections.
How is the view getting initialized? Is it a custom view type? If your view is being initialized from a nib, make sure that the class on the view type is the class that has the method implemented.
It is probably crashing because UIView does not have a method named myFunction. In order to add myFunction to a UIView object, you need to subclass it.
I assume you have subclassed your UIView, and called it something like MyView. You also probably have subclassed your UIViewController and called it MyUIViewController. Therefore you may have:
#interface MyViewController : UIViewController {
MyView *myView;
....
}
Now you can call [self.myView myFunction];
That's one way. Another way may be:
[(MyView *)self.view myFunction];
This depends on whether myView is the first view in the hierarchy (or should be set up in your NIB files).

Who should call viewDidLoad on programmatically loaded views?

When I need to load a view programmatically I do the following:
MyController* myController = [[MyController alloc] init];
[[NSBundle mainBundle] loadNibNamed:#"myNib" owner:myController options:nil];
// use my controller here, eg. push it in the nav controller
This works fine, but my controller's viewDidLoad is never called. So I resorted to manually calling it after the loadNibNamed call, but it doesn't seem correct. I was expecting the framework to call the viewDidLoad on my behalf. Is this the right way or I'm missing something?
I am new to Stack Overflow, but I discovered this question and discovered a couple other methods to load a nib file that ensure that the viewDidLoad method gets called automatically. Ed Marty's answer is correct, but it requires you to go outside the ViewController's code to load the nib file, these two examples I offer here allow you to keep your code inside the ViewController's implementation. Not necessarily better ways, just different. If you know of any drawbacks to doing it this way, please let me know your thoughts.
First, inside the initWithNibName:bundle: method of your UIViewController subclass, you can replace the call:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
with something like this: self = [super initWithNibName:#"NameOfAlternateNibFile" bundle:nibBundleOrNil];
Or, you can accomplish what appears to do exactly the same thing by the following:
[[NSBundle mainBundle] loadNibNamed:#"NameOfAlternateNibFile" owner:self options:nil];
[self setView:self.view]; //this line ensures that the viewDidLoad method is called
The key is in understanding what is written in the comments above the function definition for $initWithNibName:bundle: in the UIViewController.h file (included at the bottom of my answer here, see italics).
The nice thing about doing this using either of these methods is that viewDidLoad gets called in either scenario.
Here are the directives listed in UIViewController.h:
The designated initializer. If you
subclass UIViewController, you must
call the super implementation of this
method, even if you aren't using a
NIB. (As a convenience, the default
init method will do this for you, and
specify nil for both of this methods
arguments.) In the specified NIB, the
File's Owner proxy should have its
class set to your view controller
subclass, with the view outlet
connected to the main view. If you
invoke this method with a nil nib
name, then this class' -loadView
method will attempt to load a NIB
whose name is the same as your view
controller's class. If no such NIB in
fact exists then you must either
call -setView: before -view is
invoked, or override the -loadView method to set up your views programatically.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
You should load view controllers with
MyController* myController = [[MyController alloc] initWithNibName:#"myNib" bundle:nil];
Make sure your MyController extends from UIViewController, and the View property is set properly in Interface Builder.
The views are loaded lazyly by UIViewController. If you use the accessor myController.view in your code, the view should be loaded and viewDidLoad be called.
I noticed the same thing. I think ViewDidLoad must be called by the view CONTROLLER. Since you don't have a view controllr in your nib, you have to call the viewdidload manually. I"m having to do the same thing.