2017-03-24 7 views
0

私はこの素早いプレイグラウンド(Xcode)を実行するたびに、最後の行にエラーが表示されます。このUIButtonのアクションを正しく動作させる方法は?

アクセシブルな初期設定がないため、 '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() 

どのようにこの問題を解決できますか?

答えて

0

Swiftでは、変数をコードで使用する前に初期化する必要があるからです。このケースでは、あなたの関数内で明示的に設定するため、一方で、あなたに暗黙的に開封されたoptionals

var introImage:UIImageView! 
var introButton:UIButton! 

を宣言する可能性があり、これはあなたのコード

0

追加しますか? introImageとintroButtonの後

var introImage:UIImageView? 
var introButton:UIButton? 
+0

で他に何も変更せずに動作するはずですありがとうございます!私はあなたが言ったことを正確に行い、それは働いた。このウェブサイトもあります:https://www.ralfebert.de/snippets/ios/uikit-playground/UIButton/私のプロジェクトを助けました。 –

関連する問題