views:

189

answers:

2

I am writing a library to be used by developers for the iPhone (similar to the way that OpenFeint is implemented) and I am trying to create a ViewController with an associated XIB so that I can instantiate it in my code with

SplashScreenViewController *splashScreenViewController = [[SplashScreenViewController alloc] init];
UIWindow *topApplicationWindow = [self getTopWindow];
[topApplicationWindow addSubview:splashScreenViewController.view];

However, while this works with simple controls (UIButtons, etc), nothing shows up with my SplashScreenViewController. SplashScreenViewController is very simple:

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface SplashScreenView : UIViewController {
}

@end

and the implementation is empty. In my View XIB (SplashScreenView.xib), I have tried setting the File's Owner's class to SplashScreenViewController which didn't work, then I tried it the way I've seen it done in OpenFeint which is to add a View Controller in IB and make the main UIView a child of it and make it of class SplashScreenViewController instead. That also does not work (does not display).

I'm wondering if anyone has a good idea for what I might be missing, or if someone can recommend a walkthrough for creating new ViewControllers the way that I'm attempting to.

Thanks!

A: 

Try 2 things :

  1. Call initWithNibName not just init. Maybe the OpenFeint you were talking about were overriding the init to call initWithNibName , that's why you don't see it.

  2. Set SplashScreenViewController as your file owner, and connect his view outlet to your view in IB.

Hope it helps.

Idan
`init` just does `initWithNibName:nil bundle:nil`. when nibName is nil, the default loadView uses the class name (presumably [[self class] description] or [self className]).
tc.
That still might be the problem.Pay attention that his xib name his "SplashScreenView.xib" while his controller class is "SplashScreenViewController".I assume he handwrote the m file wrong, cause otherwise I dunno why would SplashScreenView inherit from UIViewController.
Idan
I rewrote the code to be more applicable to this question rather than using the names that I will need to refactor in my project. So, yes. the XIB name is different than the ViewController's name in the actual library. Sorry about that confusion.Your suggestion does work. OpenFeint seems to do it without using `initWithNibName:bundle:` so I will keep this question open for the time being. If anyone has any idea on how to do it without using `initWithNibName:bundle:`, I will check it off as an answer. Otherwise, I'll check this one off.
I don't get your new question - if everything is working now - what is your new problem ?You can bypass calling initWithNibName, like I stated in my answer, by overriding it to call [self init] , and within init, you call your initWithNibName.Either way, if you're using a nib file , you have to somewhere call initWithNibName. (or like tc said, it will be called with default params by calling init)
Idan
Well, I would like to see how it was done in OpenFeint where explicitly setting the nib name was not necessary, but is rather implicitly set in Interface Builder by specifying the SplashScreenViewController as the type. The reasoning is simply for convention, as I believe this is typically how devs are used to it being connected (I sure am).
A: 

Instead of [splashScreenViewController alloc], try [SplashScreenViewController alloc]. I'm surprised you didn't get a compiler warning.

tc.
I used some creativity for readability when inserting the code and I mistyped the alloc statement. It is actually [SplashScreenViewController alloc] in my code. Sorry