How can I make an view-based application that does not use a nib file? - iphone

I've choosen the "view-based" application template in Xcode. The myProjectViewController.m has the following code:
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
That sounds good. But: The template generated a myProjectViewController.xib file. What I dont get is: How does Xcode connect the ViewController with that nib? Where's the part in the template code that says: "Hey, load that myProjectViewController.xib now!"? I mean... can I just delete that not-wanted nib file without setting anything else up appropriately?

You may wish to refer to my answer to this question, where I describe the specific files that need to be altered to do completely programmatic generation of your view hierarchy.
Also, some of the references linked to in the answers to this question may be of use to you.

yep you can just delete the nib and implement that method
IB connects using the name of the class in the NIb, in IB you actually set a view as the file owner of the view. so when you call initwithnib:"nibfilename" it knows what you are talking about.
The elements are wired up using IBOutlets and IBActions

Related

What are the possibilities of getting NULL IBOutlets while loading a view.. IOS

I have been working on IOS application. Before I had single project in which all source code was there application loaded properly in that setup, now I had split that into multiple projects. After that I am facing now a problem... in ViewDidLoad, IBOutlet for buttons all are coming nil values, view also loading black colored. I am not able to guess what was the problem. Any idea about what could cause this...
I have loaded my view like this...
main =[[main_page_controller alloc] init];
if (main != NULL)
[root.navigationController pushViewController:main animated:YES];
I am not sure which part of the code do I need to post here, to make the question more understandable... Please share your suggestions..
Edit: I ran my old project and then tried with my new set up application launching successfully. I removed the application from device, and loaded using new set up only, problem again shows up. So what was there in old set up? What am I missing in new... ????
Refer to the documentation
To initialize your view controller object using a nib, you use the initWithNibName:bundle: method to specify the nib file used by the view controller. Then, when the view controller needs to load its views, it automatically creates and configures the views using the information stored in the nib file.
When initialising a view controller, and you're using a .xib file for the view, you need to call initWithNibName:bundle:. This means it'll use the xib file to create the view within loadView. At the moment, you're just using init, that will create a blank UIViewController object.
So in this case, your code would be (assuming the .xib is called "MainViewControllerView.xib" within the main bundle):
main =[[main_page_controller alloc] initWithNibName:#"MainViewControllerView" bundle:nil];
if (main) {
[root.navigationController pushViewController:main animated:YES];
}
Also sanity check your .xib file to see if all the IBOutlets are connected to what you want.
First check all your connection in xib(nib) file, if its already connected then just disconnect them , clean project (cmd+k) and then connect connection again.
take a look on this image for connection

How the deleted xib gets loaded?

I have created a class 'abc' which a subclass of UIViewController. At the time of creating it i clicked the option for creating an xib for it automaticaly. Now the xcode creates 3 files for me
1. abc.h
2. abc.m
3. abc.xib.
Now whenever i create an object of abc class like
abc *a=[abc alloc];
Even when i am not initialising the object with initiwithNibName and using it, it is loading the xib file. So how this xib file got associated with the abc object. And even if i deleted the abc.xib, then also it loads that xib file. I couldnt understand from where it is loading the xib file, if it it not present in the project space. And where the association of xib and controller is stored?
Thanks in advance.
What's going on is that the default implementation of initWithNibName:bundle: searches the Main Bundle for a Nib file that has the same name as your View Controller class. This happens whether you select the option for creating the Nib automatically or not. See UIViewController documentation (the discussion portion of initWithNibName:bundle:).
Now the initWithNibName:bundle: method is UIViewController's default initializer, which means that even if you don't use it directly (say that you use init instead) it will get called under the hood anyway.
Finally, even if you delete the Nib file from XCode, for some reason (not sure why) it doesn't get deleted from the Main Bundle (at least in the simulator). Even if you clean & build the project it stays there. The solution I use to get completely rid of the Nib file is to delete the App from the simulator, then clean & build again.
Hope this helps!
The xib is probably still in your compiled area, so you need to perform a clean to get rid of it fully. (Product >> Clean). The default init method of UIViewControllers will automatically look for a xib of the same name, which is why it's still allocating that xib. Once you clean it will stop.
Reference:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle:
Note this part: If you specify nil for the nibName parameter, you must either override the loadView method and create your views there or you must provide a nib file in your bundle whose name (without the .nib extension) matches the name of your view controller class. (In this latter case, the class name becomes the name stored in the nibName property.) If you do none of these, the view controller will be unable to load its view.

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.

iPhone Obj C - change startup root class?

I am learning iPhone Obj C slowly. I have a XIB with several views and all works well. I need to have a 2nd XIB to control another set of views but haven't been able to make it work.
So I created the 2nd class and a 2nd XIB, all called one.h one.m one.xib and the same for the new one is all two.*
As it didnt work I was going try and change the app to start on the TWO class rather than the ONE class. In the plist I changed the Main Nib base file but that didnt seem to do anything.
Where do you specify what the start up class is? That way I can make sure I did everything correctly first, and then go back to the code that is supposed to call the two class and xib.
Also if anyone has any sample code to go from one class and xib to another, please let me know.
thanks!
In your AppDelegate class make sure the ViewController being allocated and set to the window is the one desired.
Your project's [ProjectName]-Info.plist file decides which Nib file is used when the application starts, in the key NSMainNibFile. (By default, this is set to MainWindow.)
A standard MainWindow.nib file will define the "root" class, which is usually [ProjectName]AppDelegate. You can, however, change this by editing the nib.
I had to set the CLASS IDENTITY to the proper class for the app delegate on the XIB for both XIB.
Then I changed the plist to point to the XIB I wanted to start as root.
Both of the answers above helped me find this.
THANK YOU

was unable to load a nib named "TwitterDrilldownView"

-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named "TwitterDrilldownView"
I get the above error when I push a new ViewController onto the navigation stack. This is the push code,
[self.navigationController pushViewController:[[[TwitterDrilldownViewController alloc] initWithTwitterAnnotation:temp] autorelease] animated:YES];
Basically I am just pushing a newly allocated and initialized view onto the stack. The init method of the ViewController is,
- (id)initWithTwitterAnnotation:(TwitterInfo *)aPOI {
if(self = [super init]) {
poi = aPOI;
}
return self;
}
As you can see I do not use any initialize with nib method and there is no nib file named TwitterDrilldownView in my project.
I did have a nib file before I created the TwitterDrilldownViewController called TwitterDrillDownView but I was using it to test a layout and, again, never used it. When I created TwitterDrilldownViewController the TwitterDrillDownView.nib was present in the project and it was after this stage that I deleted the nib.
The only cause for this problem that I can think of is that Xcode somehow created a dependency on the nib file because the nib file and view controller are named the same(TwitterDrilldownView.nib, TwitterDrilldownViewController.m), as if it was trying to be helpful but is ultimately messing up my project.
I have tried deleting and recreating the view controller in the hope that any references will be destroyed, and removed any reference to nib files in the project but to no avail.
Has anyone please got any experience with this problem or know a possible solution?
This also happened to me after deleting a XIB file out of my project. However, I did some messing around and was able to resolve the problem.
The key point is that Xcode seems to keep some kind of reference somewhere as you create XIBs and outlets/actions within them, and deleting a XIB file manually simply orphans these references. Removing these connections one-by-one, using the interface builder (UB) and the XIB seems to de-reference them properly.
The solution is then pretty clear:
Re-create a XIB file of the same name and similar construction (including the outlets and actions you had before). (Note: If you can't remember how to re-construct the XIB's outlets and actions, just re-create the XIB file with the same name and skip to my 'if it's still failing' note below.)
Manually, one-by-one, remove all of the outlets and actions in your XIB using interface builder (IB).
Re-build the app; if it works now, you can delete the XIB.
(Note: If it's still failing, you will probably have a different error. If the error refers to key-value compliance then it will list a method in the error - and that method is your problem. Make sure you recreate the named outlet/action in IB, and remove it manually using IB. That seems to de-reference the call in Xcode/your build environment.)
Happy hunting!
UIViewController should be initialized using initWithNibName:bundle: method. In its description stated:
This is the designated initializer for
this class.
If you specify nil for the nibName
parameter and do not override the
loadView method in your custom
subclass, the default view controller
behavior is to look for a nib file
whose name (without the .nib
extension) matches the name of your
view controller class. If it finds
one, the class name becomes the value
of the nibName property, which results
in the corresponding nib file being
associated with this view controller.
So if you do not load you view controller from nib file make sure you override loadView method and set controller's view property in it.
Hope that will help.