Accessing an object class member without self.var in a UIViewController
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开发者_如何学Python:4];
It suddenly works. Why is that? Why can't I access the view variable directly?
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.
精彩评论