LoginSignup
25
25

More than 5 years have passed since last update.

[Swift]Xcode6.1で確認済み!Hello Worldの次に試したい!超簡単にpickerViewを表示してみる

Last updated at Posted at 2014-09-03

1・新規ProjectをSingleViewで作成する

ここではpickerSampleというプロジェクト名で作成しています。

スクリーンショット 2014-09-03 23.31.52.png

スクリーンショット 2014-09-03 23.32.06.png

スクリーンショット 2014-09-03 23.32.20.png

2・Main.storyboardを選択する。そしてユーティリティエリアからwebviewを選び、viewControllerへpickerを貼付ける

スクリーンショット 2014-09-03 23.33.39.png

スクリーンショット 2014-09-03 23.33.50.png

3・Connection Inspectorに移動しview Controllerへdelgateを追加する

スクリーンショット 2014-09-03 23.34.04.png

スクリーンショット 2014-09-03 23.35.42.png

4・view Controllerを以下のように修正する。

//
//  ViewController.swift
//  pickerSample
//
//  Created by g08m11 on 2014/09/03.
//  Copyright (c) 2014年 Bloc. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate {

  var stations = ["渋谷", "新宿", "六本木", "東京"]

  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.
  }


  func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
    return 1
  }

  func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
    return stations.count
  }

  func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String!{
    return "\(stations[row])"
  }



}


5・指定した「"渋谷", "新宿", "六本木", "東京"」が表示されれば完成

白い画面が数秒表示されますが、慌てずに待ちましょう。

swift_1.gif


25
25
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
25
25