ios - 如何以编程方式添加 subview

标签 ios objective-c xcode

首先,我是 IOS 编程的新手。我正在做一个项目,遇到了一个问题。

我想以编程方式在我的“表格” View 上添加一个“球” subview ,但是,球不会显示。该程序仅显示表格。什么原因?提前致谢,这是我的文件:

表.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

球.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

常量.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

最佳答案

在您的 addBall 方法中,您将新的 Ball 对象添加到 self.window。您应该将其添加到 self 中。而且您不需要调用 bringSubviewToFront:

关于ios - 如何以编程方式添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28125563/

相关文章:

ios - 处理固定 UILabel 中的长文本的最佳实践是什么

ios - Google Analytics for iOS v3 的 kGAIScreenName 未声明?

objective-c - 关于Xcodebuild设置的其他困惑(64/32位,SDK版本等)

iOS Objective C ScrollView

iphone - iPhone 4 中的多任务 Xcode 3.2 info.plist 中的 ,"application does not run in back ground"选项

ios - *** 错误 *** CGImageSource 创建时数据大小为 : 22467 - current size is only: 6139

ios - 如何设置 NSNumberFormatter 以使用 "万"(日文/中文 10,000 标记)显示数字?

python - 无法使用 SSZipArchive 在 iOS9 中解压缩大型 zip 文件 (3.3GB)

objective-c - 计算n次方根?

objective-c - 以编程方式创建视网膜屏幕截图,生成非视网膜图像