Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何制作此UIButton';操作是否正确?_Swift_Uibutton_Swift Playground - Fatal编程技术网

Swift 如何制作此UIButton';操作是否正确?

Swift 如何制作此UIButton';操作是否正确?,swift,uibutton,swift-playground,Swift,Uibutton,Swift Playground,每次我运行这个swift游乐场(在Xcode上),最后一行都会出现一个错误,上面写着: 无法构造“PlaygroundView”,因为它没有可访问的初始值设定项 我在第4行还有另一个错误: 类“PlaygroundView”没有初始值设定项 import UIKit import PlaygroundSupport class PlaygroundView:UIViewController { var introImage:UIImageView var introButton:UIButt

每次我运行这个swift游乐场(在Xcode上),最后一行都会出现一个错误,上面写着:

无法构造“PlaygroundView”,因为它没有可访问的初始值设定项

我在第4行还有另一个错误:

类“PlaygroundView”没有初始值设定项

import UIKit
import PlaygroundSupport

class PlaygroundView:UIViewController {

var introImage:UIImageView
var introButton:UIButton

override func loadView() {

    print("workspace started running.")

    //Main Constant and Variable Declarations/General Setup

    let mainView = UIView()

    //Main View Modifications & styling

    mainView.backgroundColor = UIColor.init(colorLiteralRed: 250, green: 250, blue: 250, alpha: 1)

    func addItem(item: UIView) {

        mainView.addSubview(item)

    }

    //Sub-Element Constant and Variable Declarations

    introImage = UIImageView(frame: CGRect(x: 87.5, y: -300, width: 200, height: 200))
    introImage.layer.borderColor = UIColor.black.cgColor
    introImage.layer.borderWidth = 4
    introImage.alpha = 0
    introImage.transform = CGAffineTransform(rotationAngle: -90.0 * 3.14/180.0)
    introImage.image = UIImage(named: "profile_pic.png")
    introImage.image?.accessibilityFrame = introImage.frame
    introImage.layer.cornerRadius = 100
    addItem(item: introImage)

    introButton = UIButton(frame: CGRect(x: 87.5, y: 900, width: 200, height: 30))
    introButton.alpha = 0
    introButton.setTitle("Tap to meet me", for: .normal)
    introButton.titleLabel?.font = UIFont(name: "Avenir Book", size: 20)
    introButton.backgroundColor = UIColor.black
    introButton.layer.cornerRadius = 15
    introButton.layer.borderWidth = 5

    addItem(item: introButton)

    introButton.addTarget(self, action: #selector(self.introButtonAction), for: .touchDown)

    UIView.animate(withDuration: 1.5, delay: 1, options: .curveEaseInOut, animations: {

        self.introImage.frame = CGRect(x: 87.5, y: 175, width: 200, height: 200)
        self.introButton.frame = CGRect(x: 87.5, y: 400, width: 200, height: 30)

        self.introImage.transform = CGAffineTransform(rotationAngle: 0.0 * 3.14/180.0)

        self.introImage.alpha = 1
        self.introButton.alpha = 1

    }) { (mainAnimationComped) in

        if mainAnimationComped == true {

            print("introduction animation comped.")

        }

    }


}

//User Interface Actions

func introButtonAction() {

    print("user interacted with user interface")

}

}

PlaygroundPage.current.liveView = PlaygroundView()

如何解决此问题?

添加?在introImage和introButton之后

var introImage:UIImageView?
var introButton:UIButton?

加上?在introImage和introButton之后

var introImage:UIImageView?
var introButton:UIButton?

这是因为在Swift中,在代码中使用变量之前需要初始化变量。另一方面,因为在本例中,您在函数中显式地设置了它们,所以您可能会将它们声明为隐式展开的选项

var introImage:UIImageView!
var introButton:UIButton!

这应该可以在不改变代码中任何其他内容的情况下工作,因为在Swift中,在代码中使用变量之前需要初始化变量。另一方面,因为在本例中,您在函数中显式地设置了它们,所以您可能会将它们声明为隐式展开的选项

var introImage:UIImageView!
var introButton:UIButton!

这应该可以在不改变代码中任何其他内容的情况下工作

谢谢!我完全按照你说的做了,而且成功了。还有这个网站:它帮助我完成了我的项目。谢谢!我完全按照你说的做了,而且成功了。还有这个网站:它帮助我完成了我的项目。