Swift 简单控件示例:选择器(UIPickerView)

前端之家收集整理的这篇文章主要介绍了Swift 简单控件示例:选择器(UIPickerView)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
转载请声明出处:http://www.jb51.cc/article/p-bwzyycke-ga.html
------------------------------------------------------------------------------------------

//
//  ViewController.swift
//  UIPickerViewSample
//
//  Created by jinnchang on 15/4/2.
//  Copyright (c) 2015年 Jinn Chang. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
    
    var pickerView: UIPickerView!
    
    var hourLength = 24
    var minuteLength = 60

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view,typically from a nib.
        
        pickerView = UIPickerView(frame: CGRectMake(0,self.view.frame.size.height - 400,self.view.frame.size.width,400))
        pickerView.delegate = self
        pickerView.dataSource = self
        
        self.view.addSubview(pickerView)
    }

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

    // 设置个数
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 2
    }
    
    // 设置行数
    func pickerView(pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
        if(component == 0){
            return hourLength
        }
        if(component == 1){
            return minuteLength
        }
        return 0
    }
    
    // 设置每行具体内容
    func pickerView(pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String! {
        if(component == 0){
            return "\(row)时"
        }
        if(component == 1){
            return "\(row)分"
        }
        return nil
    }
    
    // 选中行的操作
    func pickerView(pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) {
        if(component == 0){
            println("hour selected: \(row)")
        }
        if(component == 1){
            println("minute selected: \(row)")
        }  
    }

}
------------------------------------------------------------------------------------------
GitHub 上项目地址:UIPickerViewSample
控件更多相关属性方法参考:UIPickerView Class Reference 文章最后更新时间:2015年4月3日11:15:03
原文链接:https://www.f2er.com/swift/327413.html

猜你在找的Swift相关文章