Sunday, 11 August 2013

viewDidLoad is not called for rootViewController

viewDidLoad is not called for rootViewController

This is my first iOS project. I'm just following the tutorial from Try iOS
over at codeschool in my XCode Application.
I've added a button in my viewDidLoad method. I don't think this is where
it would be added normally, but it should still work. The problem is, the
method is never called. Here's my code so far:
AppDelegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Set window size & background color
CGRect viewRect = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:viewRect];
self.viewController = [[ViewController alloc] init];
UIView *view = [[UIView alloc] initWithFrame:viewRect];
view.backgroundColor = [UIColor yellowColor];
self.viewController.view = view;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
NSLog(@"Screen is %f tall and %f wide", viewRect.size.height,
viewRect.size.width);
return YES;
}...
AppDelegate.h:
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *firstButton = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
firstButton.frame = CGRectMake(100, 100, 100, 44);
[firstButton setTitle:@"Don't Click!" forState:UIControlStateNormal];
[self.view addSubview:firstButton];
}...
When the app is up and running, my background remains yellow, and there is
no button visible.
Stepping through the application, didFinishLaunchingWithOptions: hits as I
expected. When the view is initializes, I expect the viewDidLoad to run. I
may just be misunderstanding the way C "sends messages".
If any additional information might help please let me know. Thanks for
any help :)
EDIT
Does this have something to do with storyboarding? I'm in storyboard mode
but I never added any button using the GUI

No comments:

Post a Comment