ios:创建自己的视图

发布于 2024-10-21 14:28:54 字数 902 浏览 1 评论 0原文

我才刚刚开始开发 iOS 应用程序。所以我想这个问题是很菜鸟的。

我试图理解 mvc 模型在 iOS 开发中是如何工作的。

我想混合可用的视图和我自己的视图(我想使用绘图字符串等基元自己绘制)

所以

我​​创建了 MyView:UIView 类。然后我重写了drawRect方法并添加了一个简单的输出。然后我使用界面生成器将我的类添加到“ipad 显示”中。

问题是 DrawRect 方法开头的断点永远不会到达。当然,我在模拟器中得到一个空白屏幕=(

我尝试添加 ViewController 并在界面生成器中使用它,但实际上我不需要它,因为我的视图只是绘制一个常量字符串,仅此而已......

我我想我误解了一些非常严重的事情,因为查看苹果示例对我来说没有任何帮助,

这是我的代码(抱歉,忘记了):

MyView.h:

@interface MyView : UIView {
...
}

MyView.m:

...
- (void)drawRect:(CGRect)rect {

    NSString *text = @"Hello, world";

    UIFont *font = [UIFont systemFontOfSize:10];

    [[UIColor blackColor] set];
    CGPoint point;
    point = CGPointMake(0.5f, 0.5f);
    [text drawAtPoint:point withFont:font];

    [font release];
}

UPD:

我添加了一个 TabBar 只是为了确保问题得到解决。只是我自己的看法就是这样:TabBar 出现了。

I'm only starting to developing iOS apps. So the question is gonna be nooby i guess..

I'm trying to understand how the mvc model works in iOS developing.

I want to mix available views and my own (which I want to draw by myself using primitives such as drawing strings)

So

I created the MyView:UIView class. Then I overrided drawRect method and added there a simple output. Then I added my class to the "ipad display" using interface builder.

And the problem is that breakpoint in the beginning of the DrawRect method never gets reached. And of course I get a blank screen in simulator=(

I've tried to add ViewController and use it in the interface builder but in fact i don't need it because my view just draws a constant string and that's all...

I guess I misunderstand something very serious because looking through apple examples got nothing for me.

Here's my code (sorry, forgot about it):

MyView.h:

@interface MyView : UIView {
...
}

MyView.m:

...
- (void)drawRect:(CGRect)rect {

    NSString *text = @"Hello, world";

    UIFont *font = [UIFont systemFontOfSize:10];

    [[UIColor blackColor] set];
    CGPoint point;
    point = CGPointMake(0.5f, 0.5f);
    [text drawAtPoint:point withFont:font];

    [font release];
}

UPD:

I've added a TabBar just to make sure that the problem is only with my own view. That's it: TabBar appears.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

等待我真够勒 2024-10-28 14:28:54

由于您没有发布代码,所以这是一个疯狂的猜测,但您需要重新渲染视图:

[myViewInstance setNeedsDisplay:YES];

另外,请确保视图实际已加载。您可以在 nib 唤醒方法(-[awakeFromNib])中使用一点 NSLog 来检查这一点。

As you didn't post your code, this is a wild guess, but you need to make the view re-render:

[myViewInstance setNeedsDisplay:YES];

Also, make sure the view is actually loaded. You can check this with a little NSLog in the nib awaking method (-[<NSNibAwaking> awakeFromNib]).

阳光的暖冬 2024-10-28 14:28:54

如果 drawRect: 没有被调用,那么要么您实际上不在屏幕上,要么您为 drawRect: 输入了错误的签名。正确的签名是:

- (void)drawRect:(CGRect)rect

为了确保您确实在屏幕上,请尝试重载 awakeFromNib 并在那里放置一个断点。如果不是,那么您没有在界面生成器中正确配置它。最常见的问题是未能为视图设置正确的类(应设置为 MyView),或者将其放入未实际加载的 NIB 中。

当然,请确保您实际上是 UIView 的子类,没有来自编译器或 Interface Builder 的任何警告,并且运行时没有消息打印到控制台。

If drawRect: is not being called, then either you are not actually onscreen, or you have put in the wrong signature for drawRect:. The correct signature is:

- (void)drawRect:(CGRect)rect

To make sure you're actually onscreen, try overloading awakeFromNib and putting a breakpoint there. If you're not, then you have not configured it correctly in interface builder. The most common problems are failure to set the correct class for the view (it should be set to MyView), or putting it into a NIB that is not actually loaded.

And of course make sure that you are actually a subclass of UIView, that you do not have any warnings from the compiler or from Interface Builder, and that there are no messages printed to console when you run.

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