LoginSignup
71
70

More than 5 years have passed since last update.

SwiftでUIViewController

Last updated at Posted at 2014-06-03

Swiftを早速いじってみました。感想としてはすっごくRubyMotionみたいだな...という感じ。リファレンスひきながらでも言語仕様はどこかでみたことある系なので数日あれば慣れるような気がします。

ObjectiveCは基本なところがめんどくさいんだよね...と思ってRubyMotionを好んで使っている自分にはなかなか魅力的です。

ViewController.swift 

StoryboardにUILableとUIButtonを貼り付けて、アシスタントエディタをつなげたら下記のようなコードになりました。

接続に@IBOutlet、アクションには@IBAction func....と書くのですね。sender:id が sender : AnyObjectになっているくらいですかね。

スクリーンショット_2014-10-23_18_37_49.jpg

UIButtonを押すたびに1.2.3.4とカウントアップしていくだけのコードです。

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    var countNum = 1

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func actionButton(sender: AnyObject) {
        label.text = String(countNum)
        countNum++
    }

}

という事でここまでは簡単です。

追記

意外にストックされているので、最新のXcode6.1のコードに書き換えました(2014/10/23)

71
70
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
71
70