0
 let playButton: UIButton = {
    let button = UIButton()
    let image = UIImage(named: "VideoIcon.png") as UIImage?
    button.backgroundImage(for: .normal)
    button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
    button.setImage(image, for: .normal)

    return button
}()

func pressBackButton(button: UIButton) {
print("test")
    if let playVideoButtonURL = post?.videourl {

        let player = AVPlayer(url: playVideoButtonURL as URL)
        let playerLayer = AVPlayerLayer(player:player)
        playerLayer.frame = CGRect(x: 100, y: 200, width: 100, height: 100)
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.layer.addSublayer(playerLayer)
        player.play()

    }

}

even if the video code is wrong it should still print test but it doesn't which is strange. I feel like something may be wrong with the selector but i currently have no idea what could be wrong with it.

Tim
  • 3
  • 1
  • You can not access property(function) of self before self is initialized. It means you cannot access `pressBackButton` from property initializer block. – Ryan Mar 21 '17 at 21:42
  • Do you know how I would go about fixing it? I'm semi new to swift. I tried placing the function above the button but it still didn't show anything in the console. – Tim Mar 21 '17 at 21:57

2 Answers2

0

access function or property in self outside of initializer.

class TestViewController: UIViewController {
    let playButton: UIButton = {
        let button = UIButton()
        let image = UIImage(named: "VideoIcon.png") as UIImage?
        button.backgroundImage(for: .normal)
        button.setImage(image, for: .normal)

        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
    }
}
Ryan
  • 4,799
  • 1
  • 29
  • 56
  • So I tried that but if i move playbutton up there i get use of unresolved identifier pressbackbutton because it is in the class videofeedcell which is a uicollectionviewcell and it needs to be there to work. – Tim Mar 21 '17 at 22:09
  • @Tim You mean the `playButton` is in `VideoFeedCelll` and you want to invoke method in `UIViewController` when the button pressed? If so, you need to add target when you configure cell such as in `cellForItem`. – Ryan Apr 19 '17 at 06:13
0

Instead of doing:

let playButton: UIButton = {

    ...
    ...
    return button
}()

which won't work (because you're attempting to create playButton at the wrong time -- which is before the view is loaded or before it's about to appear)

In your viewDidLoad or viewWillAppear, create the button, define the target and simply add it as a subview.

override func viewDidLoad() {
  super.viewDidLoad()

  let button = UIButton()
  if let image = UIImage(named: "VideoIcon.png") as UIImage?        
  {
     button.backgroundImage(for: .normal)
     button.addTarget(self, action: #selector(pressBackButton(button:)), for: .touchUpInside)
     button.setImage(image, for: .normal)
  }
  self.addSubview(button)
}

Adding the button as a subview increments the retain count (i.e. it won't go away), but if you want to keep it around as a property simply declare it as:

var playButton : UIButton?

and then set playButton = button at the end of viewDidLoad.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215