简体   繁体   English

如何以编程方式添加子视图

[英]How to programmatically add a subview

First of all, I am pretty new to IOS programming. 首先,我对IOS编程很陌生。 I am working on a project and faced an issue. 我正在做一个项目,遇到了一个问题。

I want to add a "ball" subview over my "table" view programmatically, however, the ball will not show up. 我想以编程方式在“表”视图上添加“球”子视图,但是,该球不会显示。 The program is showing only the table. 程序仅显示表格。 What's the reason? 什么原因? Thanks in advance and here are my files: 在此先感谢,这是我的文件:

Table.m 表.m

//
//  Table.m
//  Lines
//
//  Created by Eduard Avetisyan on 1/24/15.
//  Copyright (c) 2015 Eduard Avetisyan. All rights reserved.
//

#import "Table.h"
#import "Constants.h"

@implementation Table


-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self addBall];
    }
    return self;
}

- (void)addBall
{
    Ball* myBall = [[Ball alloc]init];
    [self.window addSubview:myBall];
    myBall.center = self.window.center;
    [self.window bringSubviewToFront:myBall];
}
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetLineWidth(context, 2.0f);

    for (int i = 0;i <= TABLE_LENGTH; i++)
    {

        //draw rows
        CGContextMoveToPoint(context, BORDER_DISTANCE +i * CELL_LENGTH, BORDER_DISTANCE);
        CGContextAddLineToPoint(context, BORDER_DISTANCE + i * CELL_LENGTH, BORDER_DISTANCE + CELL_LENGTH * TABLE_LENGTH);
        CGContextStrokePath(context);

        //draw columns
        CGContextMoveToPoint(context, BORDER_DISTANCE, BORDER_DISTANCE + i * CELL_LENGTH);
        CGContextAddLineToPoint(context, BORDER_DISTANCE + CELL_LENGTH * TABLE_LENGTH, BORDER_DISTANCE + i * CELL_LENGTH);
        CGContextStrokePath(context);
    }

}

@end

Ball.m

//
//  Ball.m
//  Lines
//
//  Created by Eduard Avetisyan on 1/24/15.
//  Copyright (c) 2015 Eduard Avetisyan. All rights reserved.
//

#import "Ball.h"

@implementation Ball

-(instancetype)init
{
    self = [super init];

    if (self) {

        CGRect newFrame = self.frame;

        newFrame.size.width = 50;
        newFrame.size.height = 50;
        [self setFrame:newFrame];

        Colors color = arc4random_uniform(NUMBER_OF_COLORS);
        [self drawBall:color];
    }

    return self;
}


-(void)drawBall:(Colors)color
{
    switch (color) {
        case red:
            self.fillColor = [UIColor redColor].CGColor;
            break;
        case green:
            self.fillColor = [UIColor greenColor].CGColor;
            break;
        case yellow:
            self.fillColor = [UIColor orangeColor].CGColor;
            break;
        case blue:
            self.fillColor = [UIColor blueColor].CGColor;
            break;

        default:
            break;
    }

}

-(void)setFillColor:(CGColorRef)fillColor
{
    _fillColor = fillColor;

    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, self.fillColor);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);

    CGContextFillEllipseInRect(context, rect);
    CGContextStrokeEllipseInRect(context, rect);

}

@end

Constants.h 常数h

//
//  Constants.h
//  Lines
//
//  Created by Eduard Avetisyan on 1/24/15.
//  Copyright (c) 2015 Eduard Avetisyan. All rights reserved.
//

#ifndef Lines_Constants_h
#define Lines_Constants_h

#define TABLE_LENGTH 10
#define CELL_LENGTH 30
#define BORDER_DISTANCE 5
#define NUMBER_OF_COLORS 4

typedef enum {
    red,
    green,
    yellow,
    blue,
    black,
    orange,
    violet
}Colors;


#endif

In your addBall method, you are adding the new Ball object to self.window . addBall方法中,您addBall新的Ball对象添加到self.window You should just add it to self instead. 您应该将其添加到self And you should not need to call bringSubviewToFront: . 而且您不需要调用bringSubviewToFront:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM