0

I am reading Programming iOS4 by O'Rielly and I am using a slightly newer version of XCode than the one that the book is using. However, this slight change has led to a little bit of confusion because I cannot create window-based app using the XCode 4.2.

Anyhow, I started an empty project which gave me the barebone structure for an iPhone app without the MainWindow.xib. I was already given the project's delegate .h and .m. I proceeded to create my own MainWindow.xib. I figured out that I had to set 'Main nib file base name' to 'MainWindow' for my nib to show up at all and so I did that.

Inside my MainWindow.xib, I added a button under the window object just to make sure that I have what I want when I run the project. This is the state of my nib right now

MainWindow.xib

Without making any changes to the AppDelegate.h and AppDelegate.m, I built and ran my project. I was able to see the button! HOWEVER, I could not click on the button and when I pressed the home button and resumed my empty app, I could not see the button anymore! Here are some files that I did not make any changes to:

main.m

#import <UIKit/UIKit.h>

#import "EmptyWindowAppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([EmptyWindowAppDelegate class]));
    }
}

EmptyWindowAppDelegate.m

#import "EmptyWindowAppDelegate.h"

@implementation EmptyWindowAppDelegate

@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
//...Omitted
denniss
  • 17,229
  • 26
  • 92
  • 141

2 Answers2

0

I think the problem is with this line in AppDelegate.m:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

What's happening is when the application launches you create a new window with a blank white background and display it. Because you already designed the window in Interface Builder, you don't have to do it again in code. All you need under didFinishLaunching... is:

[self.window makeKeyAndVisible];
return YES;

Then change "@synthesize window = _window;" to just read "@synthesize window;"

The next step is changing the AppDelegate.h file. Change your code to look like this.

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{

}
@property (strong, nonatomic) UIWindow *window;

Now what you can do in InterfaceBuilder is link up the window property in AppDelegate.m with the window you designed. Once you have created the window property it will show up under Outlets for EmptyWindowAppDelegate. click and drap from the emptycirle to the Window label on the side panel, or directly to the window you have designed on the screen.

Good Luck

firescar96
  • 419
  • 3
  • 8
0

You can specify the name of the main nib file in your application plist (by default it is MainWindow.xib).

To investigate your problem further you could just create a default view based app and see what the differences are. It seems to me that you have made no connection in interface builder between your App Delegate's window property and your IB window object. Which means that window, in didFinishLaunchingWithOptions would probably be nil and that the messages you send it will therefore have no effect.

jbat100
  • 16,757
  • 4
  • 45
  • 70
  • I made the window object to be an IBOutlet and I have wired the Window nib object to reference the window IBOutlet and still no go. – denniss Nov 05 '11 at 09:52
  • And I compared my project with the templatized one. The only difference is that the templatized version has a view controller. However, I do not think that a controller is required? – denniss Nov 05 '11 at 09:54
  • I've never tried with no view controller, I don't understand why your button does not show. You can check the windows subviews and the button's superview in code, set some IBAction callbacks. In any application it's better and recommended to have a view controller though. – jbat100 Nov 05 '11 at 10:07
  • it does show. but when I tap on it,it doesnt turn blue. Yea, I understand that with a real project, a controller will be recomennded, but I just want to be able to understand how the project gets wired up from ground up. – denniss Nov 05 '11 at 10:15
  • It could be that the window's interactionEnabled is NO by default. – jbat100 Nov 05 '11 at 10:24