swift中UIView的子类化

swift中UIView的子类化,swift,uiview,Swift,Uiview,当子类化UIView时,如何访问父类、方法和属性?。。。这不起作用: // // Draw2D.swift // Draw2D // import UIKit class Draw2D: UIView { let coloredSquare = Draw2D() coloredSquare.backgroundColor = UIColor.blueColor() coloredSquare.frame = CGRect(x: 0, y: 120, width: 50, height:

当子类化
UIView
时,如何访问父类、方法和属性?。。。这不起作用:

//
//  Draw2D.swift
//  Draw2D
//
import UIKit

class Draw2D: UIView {


let coloredSquare = Draw2D()
coloredSquare.backgroundColor = UIColor.blueColor()
coloredSquare.frame = CGRect(x: 0, y: 120, width: 50, height: 50)
addSubview(coloredSquare)

}

感谢您没有为Draw2D类创建初始化器。它需要这样才能调用super.init,这反过来实际上会创建UIView,您可以从中进行子类化

您还在类中创建了另一个Draw2D实例。这是不好的,如果您在初始化器(代码所属的地方)中实际执行此操作,它将创建无限多的子视图

递归函数非常棒,递归初始化器非常糟糕;)

在调用super.init()之后,您可以从super类中调用stuff。使用self更清晰,但这不是必需的

class Draw2DCorrected: UIView {

    init() {

        let rect = CGRect(x: 0, y: 120, width: 50, height: 50)

        super.init(frame: rect)
        self.frame = rect // inherited stuff from super class -> UIView
        self.backgroundColor = UIColor.blueColor() // inherited stuff from super class -> UIView

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

var coloredSquare = Draw2DCorrected() // playground only

您没有为Draw2D类创建初始化器。它需要这样才能调用super.init,这反过来实际上会创建UIView,您可以从中进行子类化

您还在类中创建了另一个Draw2D实例。这是不好的,如果您在初始化器(代码所属的地方)中实际执行此操作,它将创建无限多的子视图

递归函数非常棒,递归初始化器非常糟糕;)

在调用super.init()之后,您可以从super类中调用stuff。使用self更清晰,但这不是必需的

class Draw2DCorrected: UIView {

    init() {

        let rect = CGRect(x: 0, y: 120, width: 50, height: 50)

        super.init(frame: rect)
        self.frame = rect // inherited stuff from super class -> UIView
        self.backgroundColor = UIColor.blueColor() // inherited stuff from super class -> UIView

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

var coloredSquare = Draw2DCorrected() // playground only

您没有为Draw2D类创建初始化器。它需要这样才能调用super.init,这反过来实际上会创建UIView,您可以从中进行子类化

您还在类中创建了另一个Draw2D实例。这是不好的,如果您在初始化器(代码所属的地方)中实际执行此操作,它将创建无限多的子视图

递归函数非常棒,递归初始化器非常糟糕;)

在调用super.init()之后,您可以从super类中调用stuff。使用self更清晰,但这不是必需的

class Draw2DCorrected: UIView {

    init() {

        let rect = CGRect(x: 0, y: 120, width: 50, height: 50)

        super.init(frame: rect)
        self.frame = rect // inherited stuff from super class -> UIView
        self.backgroundColor = UIColor.blueColor() // inherited stuff from super class -> UIView

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

var coloredSquare = Draw2DCorrected() // playground only

您没有为Draw2D类创建初始化器。它需要这样才能调用super.init,这反过来实际上会创建UIView,您可以从中进行子类化

您还在类中创建了另一个Draw2D实例。这是不好的,如果您在初始化器(代码所属的地方)中实际执行此操作,它将创建无限多的子视图

递归函数非常棒,递归初始化器非常糟糕;)

在调用super.init()之后,您可以从super类中调用stuff。使用self更清晰,但这不是必需的

class Draw2DCorrected: UIView {

    init() {

        let rect = CGRect(x: 0, y: 120, width: 50, height: 50)

        super.init(frame: rect)
        self.frame = rect // inherited stuff from super class -> UIView
        self.backgroundColor = UIColor.blueColor() // inherited stuff from super class -> UIView

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

var coloredSquare = Draw2DCorrected() // playground only

这些天,这些天使用容器视图,这些天使用容器视图,这些天使用容器视图,使用容器视图哪个致命错误?删除最后一行,那一行是运动场。(这些代码段非常容易测试。)运行代码时。你看到屏幕上出现蓝色方块了吗?没有,因为发布的代码只是
类Draw2D
。但这完全是另一个问题。非常简短:定义UIView类->创建UIView类的实例->将实例添加到superView google
addSubview
了解更多信息。哪个致命错误?删除最后一行,那一行是运动场。(这些代码段非常容易测试。)运行代码时。你看到屏幕上出现蓝色方块了吗?没有,因为发布的代码只是
类Draw2D
。但这完全是另一个问题。非常简短:定义UIView类->创建UIView类的实例->将实例添加到superView google
addSubview
了解更多信息。哪个致命错误?删除最后一行,那一行是运动场。(这些代码段非常容易测试。)运行代码时。你看到屏幕上出现蓝色方块了吗?没有,因为发布的代码只是
类Draw2D
。但这完全是另一个问题。非常简短:定义UIView类->创建UIView类的实例->将实例添加到superView google
addSubview
了解更多信息。哪个致命错误?删除最后一行,那一行是运动场。(这些代码段非常容易测试。)运行代码时。你看到屏幕上出现蓝色方块了吗?没有,因为发布的代码只是
类Draw2D
。但这完全是另一个问题。非常简短:定义UIView类->创建UIView类的实例->将实例添加到superView google
addSubview
了解更多信息。