Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 639
MrCarder Avatar asked Jun 15 '26 05:06

MrCarder


2 Answers

... self.viewController.view = view; ...

You shouldn't set viewController's view before viewDidLoad, which cause viewDidLoad not called. View should be set in viewDidLoad, and the viewController's will manage its view automatically for you.

like image 121
Nickolas Avatar answered Jun 17 '26 19:06

Nickolas


viewDidLoad is not being called because of the non-standard way that you're creating its view. Usually, the view is loaded from a xib or storyboard, or created in code in the loadView method of the view controller -- in all these cases, viewDidLoad will be called. If you move your view creation to loadView in the controller's .m file, it will work, and viewDidLoad will be called.

After Edit:

If you're using a storyboard, your view should be created there, and you shouldn't be doing what you're doing in the app delegate. In fact, when you use a storyboard, you don't normally have any code in the application:didFinishLaunchingWithOptions: method.

like image 31
rdelmar Avatar answered Jun 17 '26 18:06

rdelmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!