//
// AppDelegate.swift
// UIKit012
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
internal var window: UIWindow?
private var myNavigationController: UINavigationController?
/*
アプリケーション起動時に呼び出されるメソッド.
*/
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
// ViewControllerを生成する.
let myFirstViewController: FirstViewController = FirstViewController()
// Navication Controllerを生成する.
myNavigationController = UINavigationController(rootViewController: myFirstViewController)
// UIWindowを生成する.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
// rootViewControllerにNatigationControllerを設定する.
self.window?.rootViewController = myNavigationController
self.window?.makeKeyAndVisible()
return true
}
}
//
// FirstViewController.swift
// UIKit012
//
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Controllerのタイトルを設定する.
self.title = "My 1st View"
// Viewの背景色をCyanに設定する.
self.view.backgroundColor = UIColor.cyanColor()
// ボタンの定義をおこなう.
let myButton = UIButton(frame: CGRectMake(0,0,100,50))
myButton.backgroundColor = UIColor.orangeColor()
myButton.layer.masksToBounds = true
myButton.setTitle("ボタン", forState: .Normal)
myButton.layer.cornerRadius = 20.0
myButton.layer.position = CGPoint(x: self.view.bounds.width/2, y:200)
myButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)
// ボタンをViewに追加する.
self.view.addSubview(myButton);
}
/*
ボタンイベント
*/
internal func onClickMyButton(sender: UIButton){
// 移動先のViewを定義する.
let secondViewController = SecondViewController()
// SecondViewに移動する.
self.navigationController?.pushViewController(secondViewController, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
//
// SecondViewController.swift
// UIKit012
//
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Controllerのタイトルを設定する.
self.title = "My 2nd View"
// Viewの背景色を定義する.
self.view.backgroundColor = UIColor.greenColor()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}