drawRect 在 UIView 子类上被调用,但不在 UIViewController 上被调用
到目前为止,我的代码可以正常工作,但我必须为 UIView
创建一个类。这有点不方便,因为我也需要与 ViewController 交互。
顺便说一句,我确实在 UIViewController
子类文件的 ViewDidLoad
上尝试了 [self setNeedsDisplay]
但它不起作用。
这是代码,它适用于 UIView
子类,但不会在 UIViewController
子类上调用:
- (void)drawRect:(CGRect)rect
{
UIColor *currentColor = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
someNum = 1;
CGContextBeginPath(context);
CGContextMoveToPoint(context, 30, 40);
[self addDotImageX:30 andY:40];
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextStrokePath(context);
}
关于尝试什么有什么想法吗?顺便说一句,这是一个 TabBar 应用程序。我知道这些可以以某种方式阻止对drawRect
的调用。
选项卡以编程方式创建,而不是通过笔尖。例如:
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
UIViewController *vc;
vc = [[Education alloc] init];
vc.title = @"Education";
[listOfViewControllers addObject:vc];
vc.tabBarItem.image = [UIImage imageNamed:@"info.png"];
[vc release];
如果有任何想法,我将不胜感激。我已经浏览过此网站上与 setNeedsDisplay
未调用 drawRect
相关的答案,但尚未找到适合我的特定情况的答案。
谢谢。
My code is working so far but I had to create a Class for the UIView
. This is a bit inconvenient because I need to interact with the ViewController too.
BTW, I did try [self setNeedsDisplay]
on the ViewDidLoad
of the UIViewController
subclass file but it didn't work.
Here's the code, which works on UIView
Subclass but doesn't get called on a UIViewController
one:
- (void)drawRect:(CGRect)rect
{
UIColor *currentColor = [UIColor redColor];
CGContextRef context = UIGraphicsGetCurrentContext();
someNum = 1;
CGContextBeginPath(context);
CGContextMoveToPoint(context, 30, 40);
[self addDotImageX:30 andY:40];
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
CGContextStrokePath(context);
}
Any ideas on what to try? BTW, this is a TabBar App. I know those can somehow block the calls to drawRect
.
The Tabs where created programatically, not through a Nib. Eg:
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
UIViewController *vc;
vc = [[Education alloc] init];
vc.title = @"Education";
[listOfViewControllers addObject:vc];
vc.tabBarItem.image = [UIImage imageNamed:@"info.png"];
[vc release];
I would appreciate any ideas. I've been through the answers on this site related to setNeedsDisplay
not calling drawRect
and haven't found an answer for my particular case.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你混淆了两个类。 UIViewController 不是 UIView,这意味着它不继承自 UIView。但好消息是 有一个视图,声明为属性:它是组合。 drawRect 方法仅在 UIView 类/子类中可用。
如果你想强制控制器的视图重绘,你可以
在 viewController 中调用。
您可以使用 loadView 方法。它可能看起来像这样:
因此,您可以将绘图代码单独保存在 MySubclassOFUIView.m 文件中。
关于UIViewController:
UIView:
看看
You are mixing up two classes. A UIViewController is not a UIView, meaning it doesn't inherits from UIView. But the good news is it has a view, declared as property: It's composition. The drawRect method is only available in a UIView class/subclass.
If you like to force the controller's view to redraw you can call
in the viewController.
You can set your own custom view as the view of your viewController with the loadView method. It could look like this:
So you can keep your drawing code separated in your MySubclassOFUIView.m file.
About the UIViewController:
And the purpose of a UIView:
Have a look at Cocoa Core Competencies / Model-View-Controller in Apple's official documentation, it describes the MVC design pattern.
您无法在 UIViewController 中重写
drawRect
,因为 UIViewController 没有drawRect
方法。据我了解,您正在制作一些自定义绘图,因此您可以对 UIView 进行子类化(尽管如果您不这样做也可以获得相同的结果,那就更好了)。但是如果你想控制它的行为,那么你应该子类化 UIViewController。
确保您了解 MVC 的工作原理!
You can't override
drawRect
in a UIViewController, because UIViewController doesn't have adrawRect
method.As I understand, you're making some custom drawing, so it's ok for you to subclass UIView (though if you can have the same results without doing so, it's better). But then if you want to control its behavior, then you should subclass UIViewController.
Make sure you understand how MVC works!
在界面生成器中设置从
UIViewController
继承的类继承自
UIView
的类,并且不要重写继承自UIViewController
的类中的-drawRect:
方法。在从UIView
派生的类中定义-drawRect:
方法。Set your class's class which has been inherited from
UIViewController
in the interface builderclass which is inherited from
UIView
and don't override the-drawRect:
method in the class which has been inherited fromUIViewController
. Define the-drawRect:
method in the class which has been subclassed fromUIView
.