Created
March 16, 2015 21:32
-
-
Save ilyapuchka/1ae19259161a91f3a8a8 to your computer and use it in GitHub Desktop.
Swift Playgrounds & iOS Simulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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