Experimenting on Playground is very useful especially when we work on some interactive UI element like UIButton.
This is a simple example that show how to use UIButton and new Swift command PlaygroundSupport (XCPlaygroundPage depreciated)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// http://lab.dejaworks.com/ios-swift-3-playground-uibutton-action // Trevor D. Beydag import UIKit import PlaygroundSupport class Responder : NSObject { func action() { print("Button pressed!") } } let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 300.0, height: 600.0)) PlaygroundPage.current.liveView = containerView let responder = Responder() let button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) button.backgroundColor = UIColor.green button.setTitle("TEST", for: .normal) button.addTarget(responder, action: #selector(Responder.action), for: .touchUpInside) containerView.addSubview(button) |