I've created a UIViewController
which contains a view with a custom class that I've built. I've connected the view to the File's Owner view outlet. It renders properly and I don't have any real problems. However, I want to call a method within my view from my view controller instance, say it's called drawSomething
. So within a method in my controller, I use:
我已经创建了一个UIViewController,它包含一个视图,带有我所构建的自定义类。我已经将视图连接到文件的所有者视图outlet。它渲染正确,我没有任何真正的问题。但是,我想从我的视图控制器实例中调用一个方法,称为drawSomething。在我的控制器中的方法中,我使用:
[self.view drawSomething];
It works. But I get a pre-compiler warning because it appears to not know the methods within my custom view definition. Is my approach wrong here? I can create a local variable and cast it to make the warning go away obviously. I've imported the view's header file, so that's not the issue.
它的工作原理。但是我得到一个预编译器警告,因为它似乎不知道我的自定义视图定义中的方法。我的方法是错误的吗?我可以创建一个局部变量并对其进行强制转换,以使警告明显消失。我导入了视图的头文件,所以这不是问题所在。
I feel like I'm missing something trivial.
我觉得我错过了一些琐事。
.h of my custom view contains:
.h的自定义视图包括:
-(void) drawSomething;
.m of my custom view contains:
.m的自定义视图包含:
-(void) drawSomething { <code> };
1 个解决方案
#1
12
self.view
is basically a pointer to a UIView
used as the main view for the view controller. Since your view is a custom class with custom methods, you need a cast to your custom class to call your custom methods:
自我。视图基本上是一个指向UIView的指针,用作视图控制器的主视图。由于您的视图是一个具有自定义方法的自定义类,因此需要向自定义类强制转换以调用自定义方法:
[((MyCustomClass *)self.view) drawSomething];
Otherwise, you are calling drawSomething
on a standard UIView
, which does not exist.
否则,您正在一个标准的UIView上调用drawSomething,它并不存在。
#1
12
self.view
is basically a pointer to a UIView
used as the main view for the view controller. Since your view is a custom class with custom methods, you need a cast to your custom class to call your custom methods:
自我。视图基本上是一个指向UIView的指针,用作视图控制器的主视图。由于您的视图是一个具有自定义方法的自定义类,因此需要向自定义类强制转换以调用自定义方法:
[((MyCustomClass *)self.view) drawSomething];
Otherwise, you are calling drawSomething
on a standard UIView
, which does not exist.
否则,您正在一个标准的UIView上调用drawSomething,它并不存在。