2014年6月23日 星期一

UIPickerView programmatically using Swift language

【說明】

要使用UIPickerView必須要繼承UIPickerViewDataSource, UIPickerViewDelegate這兩個類別,在Storyboard放入Picker View的物件,並設IBOutlet,如下圖。


dataSource與delegate也要將Outlets設定View Controller上面,如下圖。


【片段程式碼】

func numberOfComponentsInPickerView(_: UIPickerView) -> Int {
        return 1
}
設定有幾個元件在PickerView裡面,此範例設定為1。

func pickerView(_: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 5
}
設定每個元件有幾個row,範例設定5個,表示有五個選項可以滑動選擇。

func pickerView(_pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString! {
        
    let stringColor = UIColor(red: 255, green: 0, blue: 0, alpha: 1)
    let attributes = [NSForegroundColorAttributeName: stringColor]
    let showString = NSMutableAttributedString(string: "\(stringValue[row])", attributes: attributes)
        
    return showString
}
這個func是在設定選項裡面的內容,上面程式碼回傳的是NSMutableAttributedString,showString 會夾帶String與attribute給UIPickerView。

func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    switch row {
        case 0...4:
            myLabel.text = "Your choose is \(stringValue[row])."
        default:
            nil
    }
}
這個func是對應到當PickerView的值被更動的時候所對應的動作。

【完整的程式範例】

//
//  ViewController.swift
//  UIPickerView
//
//  Created by Hsu,Yi-Sheng on 2014/6/23.
//  Copyright (c) 2014 yisheng. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
                            
    @IBOutlet var pickerView: UIPickerView
    @IBOutlet var myLabel: UILabel
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    //設定有幾個元件
    func numberOfComponentsInPickerView(_: UIPickerView) -> Int {
        return 1
    }
    
    //設定每個元件中有幾個row
    func pickerView(_: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 5
    }
    
    var stringValue: String[] = ["A", "B", "C", "D", "E"]
    
    //顯示PickerView內的資料
    func pickerView(_pickerView: UIPickerView!, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString! {
        
        let stringColor = UIColor(red: 255, green: 0, blue: 0, alpha: 1)
        let attributes = [NSForegroundColorAttributeName: stringColor]
        let showString = NSMutableAttributedString(string: "\(stringValue[row])", attributes: attributes)
        
        return showString
    }
    
    //PickerView變更的時候所做的動作
    func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch row {
            case 0...4:
                myLabel.text = "Your choose is \(stringValue[row])."
            default:
                nil
        }
    }

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


}


【執行結果】

       

沒有留言:

張貼留言