Skip to content

Instantly share code, notes, and snippets.

@ilyapuchka
Created March 16, 2015 21:32
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ilyapuchka/1ae19259161a91f3a8a8 to your computer and use it in GitHub Desktop.
Swift Playgrounds & iOS Simulator
import UIKit
struct MainScene {
let vc: UIViewController
let nc: UINavigationController
init(vc: UIViewController) {
self.vc = vc
self.nc = UINavigationController(rootViewController: vc)
}
}
extension UIViewController {
class func viewController(color: UIColor) -> UIViewController {
let vc = UIViewController()
vc.view = UIView(frame: UIScreen.mainScreen().bounds)
vc.view.backgroundColor = color
return vc
}
}
class ButtonHandler: NSObject {
let scene: MainScene
init(scene: MainScene) {
self.scene = scene
super.init()
}
func buttonPressed(sender:UIButton) {
println("button pressed")
let vc = UIViewController.viewController(UIColor.yellowColor())
self.scene.nc.pushViewController(vc, animated: true)
}
}
let vc = UIViewController.viewController(UIColor.greenColor())
vc.title = "title"
let button = UIButton.buttonWithType(.System) as UIButton
button.setTitle("press me", forState: UIControlState.Normal)
button.sizeToFit()
button.center = vc.view.center
vc.view.addSubview(button)
let mainScene = MainScene(vc: vc)
let handeler = ButtonHandler(scene: mainScene)
button.addTarget(handeler, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
//Run playground
let window = UIWindow(frame: UIScreen.mainScreen().bounds)
window.rootViewController = mainScene.nc
window.makeKeyAndVisible()
CFRunLoopRun()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment