views:

43

answers:

1

I have a view outlet in my view controller. It's defined like this:

IBOutlet MyView * view;

The MyView class has some instance methods in it. I try to use them in the view controller like this:

int x = [view someMethod:4];

Xcode doesn't show any warning, no runtime errors are produced, but it doesn't call the method at all. When I call the method like this:

int x = [self.view someMethod:4];

It suddenly works. Why is that? Why can't I access the view variable directly?

A: 

Your view may be non-initialized (i.e. = nil) when you call [view someMethod:4], hence nothing happens.

However, calling self.view will guarantee -loadView be called, which in turn guarantee your view to be initialized, so it works.

KennyTM
Thanks, now it's clear :)Can I call loadView explicitly in the UIViewController constructor?
Dor
@Dor: You can, but you shouldn't.
KennyTM