Do I really need a MainWindow.xib file? - iphone

I have an iPad 4.3 project that doesn't have a MainWindow.xib because it's main view is created programmatically. Everything seems to work fine but I noticed that in my plist file there's a value for a NSMainNibFile key that has "MainWindow" for value. If I change in any way that entry, my app won't work. Now, that's strange, at least for me...

If you remove the xib reference in the target properties, be sure to change your code in main.m to refer to your class delegate class.
int retVal = UIApplicationMain(argc, argv, nil, #"YourAppDelegateClassName");

If you remove the NSMainNibFile key from your Info.plist, you need to update the code for main.m. You will need to update the NSString *delegateClassName in the call to UIApplicationMain.

No you don't necessarily need something called MainWindow.xib you could rename everywhere but as this answer points out you have to have something for the application to start with; What's the MainWindow.xib nib file for?

Related

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 does Xcode decide which xib to load first? How can I change that?

I have downloaded an Xcode project that has three xibs (A.xib, B.xib, and C.xib). I noticed that:
None of them is called "MainWindow.xib".
In the project summary the Main Interface value is set to MainWindow
Main nib file base name is not set
Main.c does not specify the app delegate either int retVal = UIApplicationMain(argc, argv, nil, nil);
Still, when I build&run, the app loads A.xib.
How does the app decide what to load?
I am using Xcode 4 and I am targeting iOS 5.
Thank you.
You can set the Main Interface in the Summary tab of your app target.
See this image:
EDIT: Or, as Alan wrote, you can set it in your info.plist file.
EDIT 2: Oh. I see what your problem is. You can use MainWindow as your main .xib file, but you have to edit which .xib is loaded in MainWindow. Open MainWindow.xib, and select the view controller that is used. If I'm right, its class is set to A. If you want B.xib to be used, you have to change the class to B. You will also have to set the NIB being used for the view controller, in the Attribute Inspector.
Set the class for the UIViewController being used in MainWindow.xib:
Set the NIB:
In your application's Info.plist.

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

'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the GameView nib but the view outlet was not set

This is not the same situation as the multitude of other similar questions here.
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the GameView nib but the view outlet was not set.'
You might be thinking "do as it says, connect the File's Owner to the View in IB!". But the thing is, I don't even HAVE a GameView.xib in my project or even in the project directory.
I do have a "GameViewController.m" and matching "GameViewController.xib" in my project. Using that GameViewController is what brings up this error, but I don't understand where it gets the idea to try and load "GameView.xib". Shouldn't it use "GameViewController.xib" instead?
If I grep my project directory, I do see it referenced from "UserInterfaceState.xcuserstate".
<string>file://localhost/Users/bemmu/Dropbox/b2/iphone/ValleyStory/ValleyStory/GameView.xib</string>
This mentioned file does not exist. I might have had a file with that name before and renamed/deleted it, but it's not being referenced to from anywhere that I can see in IB.
Did I manage to confuse xcode?
My solution was a little different.
Click on the xib in interface builder
Select File's Owner on the left
Open the File's Owner's connections inspector
If the view property isn't yet wired, control-drag it to the view icon (under the file's owner and first responder icons).
Check any nib files you're using (like MainWindow.xib). If you are loading GameViewController from a nib, check the file it's loading from (under the info tab in the inspector). Make sure it's set to "GameViewController" and not "GameView".
I had this issue as well, but had to solve it a different way. Basically, I have a view controller name MainViewController, which has a xib named MainViewController.xib. This nib has it's view property set to the File Owner which was MainViewController.
I also made a MainView.xib that contained a view that was going to be programmatically added to the view defined in MainViewController.xib and it's view. It basically encapsulated an internal view that would be in the MainViewController.xib's view, and also had it's File Owner set to MainViewController.
So basically, I wanted MainViewController.xib to load as the nib for the MainViewController object, and inside MainViewController, at some later point, I would add the internal view specified by MainView.xib.
A couple issues arose:
1.) I found in the Apple docs that when loading a view controller via storyboard or nib:
"If the view controller class name ends with the word “Controller”, as
in MyViewController, it looks for a nib file whose name matches the
class name without the word “Controller”, as in MyView.nib.
It looks for a nib file whose name matches the name of the view
controller class. For example, if the class name is MyViewController,
it looks for a MyViewController.nib file."
Therefore, you cannot have a nib called MainView.xib if you also have a nib called MainViewController and want MainViewController.xib to be the primary nib for MainViewController.
2.) Even if you delete MainView.xib or rename it to something else (MainInternalView.xib in this case), you MUST delete / clean your iOS simulator as the old nib file (MainView.xib) will still remain in the application. It doesn't overwrite the whole application package when you rebuild / rerun your application.
If you don't want to reset your content settings (perhaps you have some data you want to preserve), then right-click on your application in your iOS Simulator folder, Show Package Contents, find MainView.nib, and delete it. Xcode will NOT do this automatically for you when you rebuild, so we need to manually remove the old nib.
Overall, don't make nibs named MainViewController and MainView, i.e. nibs with the same prefix. Call MainView.xib something else, like MainInternalView.xib.
I recently solved this issue. Make sure you back up your project before following the steps given here (just in case). These steps solved my issue
Quit Xcode
Navigate to UserInterfaceState.xcuserstate located at .xcodeproj/project.xcworkspace/xcuserdata/<username>.xcuserdata and delete the file.
Reopen Xcode. Xcode will create a new UserInterfaceState.xcuserstate which will be clean.
In my case this error was produced by dumb mistake - I delete _view view
In my case, I was not using a xib at all. I needed remove the .m file from Build Phases > Compile Sources and added it back.
Given you referenced it previously it sounds like xcode hasn't ackowledged it no longer exists. From the Product menu select "Clean" and then "Build" hopefully this will get past the old reference for you.
Face the same Problem, had to change the view's name in code:
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"WrongViewName" bundle:nil];
To
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"RightViewName" bundle:nil];
I had multiple views, and by accident (I don't know how this happenned) but my background view didn't have a file owner, so for anyone else who has this problem in the future, make sure all your views have a file owner.
I was gettint the same error then check the classname from interface builder and see that I typed the view controller class name at the custom class attribute.
UIViewController searches for a nib with the same name as the controller when passed nil to initWithNibNamed:bundle: Check that the file name that you pass to the initializer is correct and exists!
For example:(e.g. [[CCVisitorsController alloc] initWithNibName:nil bundle:nil] then UIViewController tries to load nib with name CCVisitorsController as default.
If that file does not exist then the error you mentioned is thrown.
I had this problem because I was doing something bad in
(id) initWithCoder:(NSCoder *) coder
which the NIB loads.

Strange behavior with MainWindow.xib, app will not reach didFinishLaunchingWithOptions

I've started a new project in Xcode 3.2.5 where everything is being done in code. I don't want to use xibs this time though am looking forward to an integrated IB in Xcode 4.
The problem came up when I edited a string in a json file. On running the code I noticed the old string appearing even though the file was certainly changed, so I deleted my Library/Application Support/iPhone Simulator folder, expecting that my json changes would then be loaded to the bundle and the code would then see the expected value.
Then command-Y to build and run and it wouldn't run. Although there is no MainWindow.xib in my project folder my Info.plist file refers to MainWindow.xib. (I removed it, following the creation of this iPhone app with Xcode's New Project template) So I removed that key from the plist figuring the xib must have been sitting around in the app bundle after being installed in the simulator the first time I ran the app.
Then I ran the app again. No complaints, only a black screen - and a breakpoint in my app delegate, on didFinishLaunchingWithOptions is not being hit.
I have not changed anything in main.m. How can it be that the app is not launching?
I think something got messed up when you removed MainWindow.xib. Try these steps to see if it'll correct the problem.
Under the Project menu, choose Edit Active Target. Select the Properties tab and blank out the Main Nib File field. Close the Target Info window.
Under the Other Sources group and edit the main.m file. In the UIApplicationMain function call, change the last parameter to the name of your application delegate class.
int retVal = UIApplicationMain(argc, argv, nil, #"AppDelegate");
In your application delegate you need to create a window object. I do this in -applicationDidFinishLaunching:. If you defined anything else in your MainWindow.xib, you can probably create that here, too.
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
UIWindow *appWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window = appWindow;
[appWindow release];
}
If this doesn't work, let me know and I can give you another way.
Sounds like you dont have a view connected in the XIB.
This is the same as #"AppDelegate" but you can write like this too :
(argc, argv, nil, NSStringFromClass([AppDelegate class]))