iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 15
0
自我挑戰組

Swift!從新手到微上手系列 第 15

Swift-UIPickerView & UIDatePicker

如何建立UIPickerView

  • 直接在Library拉一個Picker View,再聯繫outlet

  • 用程式實作
// 取得螢幕的尺寸
let fullScreenSize = UIScreen.mainScreen().bounds.size

// 建立 UIPickerView 設置位置及尺寸
let myPickerView = UIPickerView(frame: CGRect(
  x: 0, y: fullScreenSize.height * 0.3,
  width: fullScreenSize.width, height: 150))

// 新增另一個 UIViewController 
// 用來實作委任模式的方法
let myViewController = MyViewController()

// 必須將這個 UIViewController 加入
self.addChildViewController(myViewController)

// 設定 UIPickerView 的 delegate 及 dataSource
myPickerView.delegate = myViewController
myPickerView.dataSource = myViewController

// 加入到畫面
self.view.addSubview(myPickerView)

UIPickerView的協定

class MyViewController: UIViewController,
  UIPickerViewDelegate, UIPickerViewDataSource {
}

加上這兩個protocol後需實作的方法如下

// UIPickerViewDataSource 必須實作的方法:
// UIPickerView 有幾列可以選擇
func numberOfComponentsInPickerView(
  pickerView: UIPickerView) -> Int {
    return 
}

// UIPickerView 各列有多少行資料
func pickerView(pickerView: UIPickerView,
  numberOfRowsInComponent component: Int) -> Int {
    return 
}
  • 年月日為列數
  • 有多少年、多少月、多少日為行數

// UIPickerView 每個選項顯示的資料
func pickerView(pickerView: UIPickerView,
  titleForRow row: Int, forComponent component: Int) 
  -> String? {
    return
}

  • 依照你的array去決定要選取的內容會顯示什麼

實作UIPickerView 與 UITextField 結合應用

  • 建立property
let meals = ["早餐","午餐","晚餐","宵夜"]
  • 建立meal的PickerView
let myTextField = UITextField(frame: CGRect(
            x: 0, y: 0,
            width: UIScreen.main.bounds.width, height: 40))
        
// 建立 UIPickerView
let myPickerView = UIPickerView()
        
// 設定 UIPickerView 的 delegate 及 dataSource
    myPickerView.delegate = self
    myPickerView.dataSource = self
        
// 將 UITextField 原先鍵盤的視圖更換成UIPickerView
    myTextField.inputView = myPickerView
        
// 設置 UITextField 預設的內容
    myTextField.text = meals[0]
        
// 設置 UITextField 的 tag 以利後續使用
    myTextField.tag = 100
        
// 設置 UITextField 其他資訊並放入畫面中
    myTextField.backgroundColor = UIColor.init(
        red: 0.9, green: 0.9, blue: 0.9, alpha: 1)
    myTextField.textAlignment = .center
    myTextField.center = CGPoint(
        x: UIScreen.main.bounds.width * 0.5,
        y: UIScreen.main.bounds.height * 0.15)
    self.view.addSubview(myTextField)
  • 實作PickerView的protocol
func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
// UIPickerView 各列有多少行資料
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
// 返回陣列 meals 的成員數量
    return meals.count
    }
    
// UIPickerView 每個選項顯示的資料
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
// 設置為陣列 meals 的第 row 項資料
    return meals[row]
    }
    
// UIPickerView 改變選擇後執行的動作
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// 依據元件的 tag 取得 UITextField
    let myTextField = self.view?.viewWithTag(100) as? UITextField
// 將 UITextField 的值更新為陣列 meals 的第 row 項資料
    myTextField?.text = meals[row]
    }
  • 完成的作品如下

補充:常用的時間格式

  • 以下列出DateFormatter常使用到的日期時間顯示的格式:

yyyy:西元年份,像是 2015、1998。
yy:西元年份末兩位數,像是 15、95。
MMMM:月份,像是 December、January。
MMM:月份簡寫,像是 Oct、Feb。
MM:以數字代表月份,像是 08、12。
dd:日期,像是 07、31。
EEEE:星期幾,像是 Saturday、Monday。
EEE:星期幾的簡寫,像是 Sun、Wed。
HH:24 小時制的時。
hh:12 小時制的時。
mm:分。
ss:秒。


上一篇
Swift-Tap bar controller
下一篇
Swift-Scroll View
系列文
Swift!從新手到微上手30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言