开发者

Accessing an object class member without self.var in a UIViewController

开发者 https://www.devze.com 2022-12-19 00:20 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消