Skip to content

Instantly share code, notes, and snippets.

@edwardinubuntu
Created November 8, 2016 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edwardinubuntu/6f13f7bb54c3771d915330665e51a735 to your computer and use it in GitHub Desktop.
Save edwardinubuntu/6f13f7bb54c3771d915330665e51a735 to your computer and use it in GitHub Desktop.
tibame.com iOS Swift 界面元件 3 - PickerView

實作一個 PickerView,可以選取滾輪內容,讀取顯示在螢幕上。

在 Storyboard 要完成:

  • UILabel
  • UIPickerView
//
// ViewController.swift
// PickerView
//
// Created by Edward Chiang on 02/11/2016.
// Copyright © 2016 TKU. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var dataArray = [String]()
@IBOutlet weak var resultLabel: UILabel!
@IBOutlet weak var pickerView: UIPickerView!
override func awakeFromNib() {
dataArray = ["Apple", "Banna", "Orange"]
}
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 pickerClicked(_ sender: Any) {
self.resultLabel.text = dataArray[self.pickerView.selectedRow(inComponent: 0)]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataArray[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataArray.count
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment