Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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?
 
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?

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.
 
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
 
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
 
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.
 
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...
 
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];
 
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];

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.
 

Attachments

  • TestVCInit.zip
    17.5 KB · Views: 88
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.
 
hum... i was under the impression that i was retaining the color by setting it's property (nonatomic, retain)?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.