【问题标题】:Xcode: Value of type 'GameView' has no member 'view'Xcode:“GameView”类型的值没有成员“view”
【发布时间】:2019-08-14 09:49:11
【问题描述】:

我尝试使用 Xcode 游乐场制作一些东西。当我尝试使用自动布局功能时,我收到一条错误消息显示 “GameView”类型的值没有成员“view”

这是我的代码,还有一些,但我只添加了相关的部分。

import Foundation
import UIKit
import SpriteKit
public class GameView : UIView{
self.frame.size.width, height: self.frame.size.height))
    override public init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: 1500, height: 1000))

    }
    public override func addConstraints(_ constraints: [NSLayoutConstraint]) {
        self.view.addConstraints([
            NSLayoutConstraint(item: GameView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 64),
            NSLayoutConstraint(item: GameView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64),
            NSLayoutConstraint(item: GameView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0),
            NSLayoutConstraint(item: GameView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0),
            ])
    }
}

提前致谢!

【问题讨论】:

  • 您可能永远不会将GameView 类分配给任何UIview。请您在调试时显示您的控制台结果...

标签: ios swift xcode


【解决方案1】:

使用“self”而不是“self.view”。 你应该使用 GameView.self。

import Foundation
import UIKit
import SpriteKit
public class GameView : UIView{
    override public init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: 1500, height: 1000))

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    public override func addConstraints(_ constraints: [NSLayoutConstraint]) {
        self.addConstraints([
            NSLayoutConstraint(item: GameView.self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 64),
            NSLayoutConstraint(item: GameView.self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64),
            NSLayoutConstraint(item: GameView.self, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0),
            NSLayoutConstraint(item: GameView.self, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0),
            ])
    }
}

【讨论】:

    【解决方案2】:

    它没有view 属性,我想你只想使用self。您可能从某种 UIViewController 复制了这段代码。

    【讨论】:

    • 啊,是的,我做到了,那么您建议做什么?我尝试分别删除它们并弹出一串错误。
    • 这段代码看起来有点乱,你应该在UIViewController子类中使用接口生成器或代码中的loadView方法设置约束。
    猜你喜欢
    • 1970-01-01
    • 2021-01-05
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多