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

查看:15
本文介绍了在 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];
...
}

多边形视图.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天全站免登陆