Search code examples
swiftuibuttonswift-playground

How to make this UIButton's action operate correctly?


Every time I run this swift playground (on Xcode), I get an error on the last line saying:

'PlaygroundView' cannot be constructed because it has no accessible initialisers.

I also have another error on line 4 saying:

class 'PlaygroundView' has no initializers.

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()

How would I fix this problem?


Solution

  • That's because in Swift, you need to initialize variables before use them in the code. On the other hand, since in this case you set them explicitly in your function, you might declare them as implicitly unwrapped optionals

    var introImage:UIImageView!
    var introButton:UIButton!
    

    and this should work without changing anything else in your code