drawRect 在 UIView 子类上被调用,但不在 UIViewController 上被调用

发布于 2024-11-06 13:09:47 字数 1267 浏览 0 评论 0原文

到目前为止,我的代码可以正常工作,但我必须为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素食主义者 2024-11-13 13:09:47

你混淆了两个类。 UIViewController 不是 UIView,这意味着它不继承自 UIView。但好消息是 有一个视图,声明为属性:它是组合。 drawRect 方法仅在 UIView 类/子类中可用。

如果你想强制控制器的视图重绘,你可以

[self.view setNeedsDisplay];

在 viewController 中调用。

您可以使用 loadView 方法。它可能看起来像这样:

- (void)loadView
{  
    MySubclassOfUIView *rootView = [[MySubclassOfUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // do more view configuration ...

    self.view = rootView;
    [rootView release];
}

因此,您可以将绘图代码单独保存在 MySubclassOFUIView.m 文件中。

关于UIViewController

UIViewController类提供
基本视图管理模型
适用于 iPhone 应用程序。基本的
视图控制器类支持
相关视图的呈现,
支持管理模态视图,以及
支持旋转视图作为响应
设备方向更改。

UIView

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

[self.view setNeedsDisplay];

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:

- (void)loadView
{  
    MySubclassOfUIView *rootView = [[MySubclassOfUIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // do more view configuration ...

    self.view = rootView;
    [rootView release];
}

So you can keep your drawing code separated in your MySubclassOFUIView.m file.

About the UIViewController:

The UIViewController class provides
the fundamental view-management model
for iPhone applications. The basic
view controller class supports the
presentation of an associated view,
support for managing modal views, and
support for rotating views in response
to device orientation changes.

And the purpose of a UIView:

The UIView class defines a rectangular
area on the screen and the interfaces
for managing the content in that area.
At runtime, a view object handles the
rendering of any content in its area
and also handles any interactions with
that content.

Have a look at Cocoa Core Competencies / Model-View-Controller in Apple's official documentation, it describes the MVC design pattern.

壹場煙雨 2024-11-13 13:09:47

您无法在 UIViewController 中重写 drawRect,因为 UIViewController 没有 drawRect 方法。

据我了解,您正在制作一些自定义绘图,因此您可以对 UIView 进行子类化(尽管如果您不这样做也可以获得相同的结果,那就更好了)。但是如果你想控制它的行为,那么你应该子类化 UIViewController。

确保您了解 MVC 的工作原理!

You can't override drawRect in a UIViewController, because UIViewController doesn't have a drawRect 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!

南冥有猫 2024-11-13 13:09:47

在界面生成器中设置从 UIViewController 继承的类
继承自 UIView 的类,并且不要重写继承自 UIViewController 的类中的 -drawRect: 方法。在从 UIView 派生的类中定义 -drawRect: 方法。

Set your class's class which has been inherited from UIViewController in the interface builder
class which is inherited from UIView and don't override the -drawRect: method in the class which has been inherited from UIViewController. Define the -drawRect: method in the class which has been subclassed from UIView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文