在IOS中以编程方式创建视图(它是如何工作的)? [英] Programmatically creating Views in IOS (how does it work)?

查看:162
本文介绍了在IOS中以编程方式创建视图(它是如何工作的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一点背景:我正在浏览CS193P的iTune视频,并且我被困在作业3上的时间最长。基本上,作业要求您以编程方式创建自定义视图以在屏幕上显示形状。顺便说一下,我没有使用任何视图控制器。

A little background: I'm going through the CS193P iTune videos and I was stuck on the assignment 3 for the longest time. Basically, the assignment asks you to programmatically create a custom view to display a shape on the screen. I'm not using any view controllers by the way.

在我最终拖动Interface Builder中的View对象并更改对象名称之前,我无法显示我的视图到我的自定义视图类。所以我的问题是当人们说以编程方式创建视图时,他们只是说手动创建类,但是当你需要显示它时使用IB吗?我不禁觉得我误解了什么?

I could not get my view to display until I finally dragged a View object in Interface Builder and change the object name to my custom view class. So my question is when people say to programmatically create a view, are they just saying manually create the class but when you need to display it use IB? I can't help feeling like I misunderstood something?

编辑:让我更清楚。我的自定义视图已使用0,0,200,150的框架初始化,并且drawRect被覆盖以在其中绘制正方形。如果尝试将其添加到我的控制器中的主窗口,我的视图甚至都不显示:

edit: let me be more clear. My custom view has been initialized with a frame of 0, 0, 200, 150 and drawRect is overriden to draw a square in it. My view doesn't even show up if try adding it to the main window within my controller:

    UIWindow* window = [UIApplication sharedApplication].keyWindow;
[window addSubview:polygonView];

但是,如果使用在IB中拖动视图并将类更改为我的视图类,则会显示很好。

However, if use drag a view in IB and change the class to my view class, it shows up fine.

编辑:添加了一些代码。这是我的控制器的awakeFromNib方法,其中应该绘制视图。

Added some code. This is my controller's awakeFromNib method where the view should be drawn.

    - (void)awakeFromNib {
    shape = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLable.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
    polygonView = [[PolygonView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    polygonView.backgroundColor = [UIColor blackColor];
    [window addSubview:polygonView];
    [self updateInterface];  
}

我的控制器的updateInterface方法的一部分:

Part of my controller's updateInterface method:

- (void)updateInterface {
    [polygonView setPolygon:shape];
    [polygonView setNeedsDisplay];
...
}

PolygonView.h

PolygonView.h

#import <UIKit/UIKit.h>
#import "PolygonShape.h"

@interface PolygonView : UIView {
    IBOutlet PolygonShape *polygon; 
}

@property (readwrite, assign) PolygonShape *polygon;

- (void)drawRect:(CGRect)rect;
@end

PolygonView.m

PolygonView.m

#import "PolygonView.h"

@implementation PolygonView
@synthesize polygon;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        nslog(@"initialized");
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
       CGRect bounds = [self bounds];

    [[UIColor grayColor] set];
    UIRectFill(bounds);

    CGRect square = CGRectMake(10, 10, 10, 100);
    [[UIColor blackColor] set];
    UIRectFill(square);

    [[UIColor redColor] set];
    UIRectFill(square);
    NSLog(@"drawRect called");
}
@end

正在初始化polygonView但是drawRect是'被调用。

The polygonView is being initialized but the drawRect isn't being called.

推荐答案

为了更具体地解决你的问题,语法将是

To be even more specific to your question, the syntax would be

UIWindow* window = [UIApplication sharedApplication].keyWindow;

UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 200, 150)];
//add code to customize, e.g. polygonView.backgroundColor = [UIColor blackColor];

[window addSubview:polygonView];
[polygonView release];

这是一种模式,不仅可用于此,还可用于子视图。此外,另一个注意事项是许多模板,viewController已经设置了它自己的视图。当你想创建一个自定义视图时,你可以像上面那样创建它,但不是上面的方法,而是将viewControllers视图设置为新创建的视图,如下所示:

This is a pattern you will use for not only this but subviews afterwards. Also, another note is with many of the templates, the viewController is already set up with it's own view. When you want to make a custom view, you create it like above but instead of the method above you set the viewControllers view to the newly created view like so:

viewController.view = polygonView;

祝你好运!

这篇关于在IOS中以编程方式创建视图(它是如何工作的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆