ViewController Initializer?

Discussion in 'iOS Programming' started by Darkroom, Jun 26, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    i have a UIViewController set to viewWillAppear:(BOOL)animation as it will be need to refresh each time it appears on screen. the view is without a nib file, completely programatically set.

    i can't see to get the initializer correct. the documentation tells me to use - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil, but it's the code inside this init method doesn't get called.

    for testing purposes, i though maybe viewWillAppear could not substitute loadView, as is used for programatically created UIViewControllers, but changing it to loadView doesn't help either.

    so my questions:
    1. what initializer method do i use for programatically created UIViewControllers
    2. will loadView cause the view to update each time the view appears?
     
  2. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #2
    You just use init.

    Code:
    
    UIViewController *vc=[[UIViewController alloc] init];
    
    loadView only gets called once, so when the view controller is first displayed. For what you want to do viewWillAppear: is correct.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    it doesn't work

    Code:
    - (id)init
    	{
    	if (self = [super init])
    		{
    		NSLog(@"Triggered Before loadView");
    		}
    	return self;
    	}
    
    - (void)loadView
    [COLOR="Green"]//load the view[/COLOR]
    
    i'm trying to set values to some instance variables concerning the view, and they need to be set before the view loads
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    it seems i can use viewWillAppear:(BOOL)animated as an initializer to my loadView method... but is this a legal way of initializing values for my view?

    Code:
    - (void)viewWillAppear:(BOOL)animated
    	{
            [COLOR="Green"] //set values of instance variables[/COLOR]
            [super viewWillAppear:animated];
    	}
    
    - (void)loadView
    	{
             [COLOR="Green"]//load the view with the values[/COLOR]
            }
    
    [edit] nevermind, that didn't work either... NSLog appears, but values don't really work
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    You can call initWithNibName and just pass nil to the two parameters. UIViewcontroller init will call [self initWithNibName:nil bundle:nil]. So you can call [[MyViewController alloc] init] if you want to create the view controller (not [UIViewController alloc] init], which will obviously create a plain view controller and not your subclass).

    If you implement init and initWithNibName in your view controller certainly one of them will be called.

    BTW, this is where understanding what a designated initializer is would come in handy.
     
  6. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #6
    are you telling me to call the initWithNibName from my loadView method?

    since i'm not following you, i think i may have asked incorrectly. here's my code:

    Code:
    @interface testController : UIViewController
    	{
    	UIColor *theColor;
    	}
    
    @property (nonatomic, retain) UIColor *theColor;
    
    Code:
    @implementation testController
    @synthesize theColor;
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    	{
    	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    		{
    		theColor = [UIColor blackColor];
    		}
    	return self;
    	}
    
    - (void)loadView
    	{
    	CGRect fullScreenRect = [[UIScreen mainScreen] bounds];
    
    	UIView *aView = [[UIView alloc] initWithFrame:fullScreenRect];
    	aView.backgroundColor = theColor;
    	self.view = aView;
    	[aView release];
    	}
    
    it doesn't work...
     
  7. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #7
    You can call either [[testController alloc] init] or [[testController alloc] initWithNibName:nil bundle:nil]. They will both have pretty much the same effect.

    When you say 'it' doesn't work you don't say 'what' doesn't work.

    What I see in your code is that you aren't retaining theColor, which probably causes a crash or other failure in loadView. Try this

    Code:
    self.theColor = [UIColor blackColor];
     
  8. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #8
    hum... i was under the impression that i was retaining the color by setting it's property (nonatomic, retain)?

    writing self.theColor in the init method didn't help...

    i've attached the small sample project. it's has an app delegate and a UIViewController class. the .xib for the UIViewController has been deleted.

    my understanding of the chain of events is that the initWithNibName is called prior to loadView (or viewDidLoad, etc.). you will see that the assignment of theColor variable inside the initWithNibName method is not working because when the loadView fires, the view doesn't change color as i am expecting it to.

    please help me understand the initWithNibName method.
     

    Attached Files:

  9. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #9
    Your view controller instance itself is being loaded from a nib (your mainwindow.nib). So you're not going to get an initWithNibName:bundle:

    Your instance will get initWithCoder and awakeFromNib though. And both of those will be before loadView is called. So you could use those.
     
  10. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #10
    Yes, but you need to use the generated property methods either using dot notation or explicitly calling setColor which are the methods that do the retain logic. In your example you are just assigning the auto-released color to the instance variable directly, bypassing your properties.
     

Share This Page