ios swift UIPickerView的使用

文章目录

示例1:

//
//  dialogModeVC.swift
//  blueTooth
//
//  Created by macvivi on 2018/8/2.
//  Copyright © 2018年 macvivi. All rights reserved.
//

import UIKit

//Character扩展代码



class passwordSixHusVC: UIViewController,UIGestureRecognizerDelegate,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var selectedStr:String = "";
    var component0:Int = 0;
    var component1:Int = 0;
    var component2:Int = 0;
    var component3:Int = 0;
    var showStr:String = "";
    
    //闭包传值
    typealias closureBlock = (String)->Void;
    var postValueBlock:closureBlock?
    
 
    
    @IBOutlet var rootViewTap: UITapGestureRecognizer!
    
    
    @IBOutlet weak var parentView: UIView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!
    
    
    @IBAction func rootViewTapClick(_ sender: Any) {
        popVC();
    }
    
    @IBAction func cancelBtnClick(_ sender: Any) {
        popVC();
        
    }
    
    @IBAction func sureBtnClick(_ sender: Any) {
        
        cameraBlueTooth.sharedInstance.passWord1 = UInt8(component0);
        cameraBlueTooth.sharedInstance.passWord2 = UInt8(component1);
        cameraBlueTooth.sharedInstance.passWord3 = UInt8(component2);
        cameraBlueTooth.sharedInstance.passWord4 = UInt8(component3);
        cameraSixHus.sharedInstance.passWordSend();
      
        selectedStr = String(component0)+String(component1)+String(component2)+String(component3);
        postValueBlock!(selectedStr);
        popVC();
    }
    
    func popVC() {
        self.dismiss(animated: true, completion: nil);
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView.dataSource = self as! UIPickerViewDataSource;
        pickerView.delegate = self as! UIPickerViewDelegate;
        print(showStr);
        
        titleLabel.text = "Password";
        
        component0 = Int(cameraBlueTooth.sharedInstance.passWord1);
        component1 = Int(cameraBlueTooth.sharedInstance.passWord2);
        component2 = Int(cameraBlueTooth.sharedInstance.passWord3);
        component3 = Int(cameraBlueTooth.sharedInstance.passWord4);
        
        pickerView.selectRow(component0+1000, inComponent: 0, animated: false);
        pickerView.selectRow(component1+1000, inComponent: 1, animated: false);
        pickerView.selectRow(component2+1000, inComponent: 2, animated: false);
        pickerView.selectRow(component3+1000, inComponent: 3, animated: false);
        
        
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        
        return 4;
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
      
      return 2000
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        
        return String(row%10);
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        
        switch component {
        case 0:
            component0 = row%10;
        case 1:
            component1 = row%10;
        case 2:
            component2 = row%10;
        case 3:
            component3 = row%10;
        default:break;
        }
        
        
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        if (touch.view?.isDescendant(of: parentView))! {
            return false;
        }else{
            return true;
            
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
    }
    /*     // MARK: - Navigation
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

示例2:

//
//  dialogModeVC.swift
//  blueTooth
//
//  Created by macvivi on 2018/8/2.
//  Copyright © 2018年 macvivi. All rights reserved.
//

import UIKit

enum timeLapseTypeEnem{
    case switch1Item1
    case switch1Item2
    case switch2Item1
    case switch2Item2
    case switch3Item1
    case switch3Item2
    case switchtotal
}
class timeLapseVC: UIViewController,UIGestureRecognizerDelegate,UIPickerViewDataSource,UIPickerViewDelegate {
    
    var selectedStr:String = "";
    var row0:Int = 0;
    var row1:Int = 0;
    var showStr:String?;
    
    //闭包传值
    typealias closureBlock = (String)->Void;
    var postValueBlock:closureBlock?
    
    var type:timeLapseTypeEnem?;
   
    
    
    @IBOutlet var rootViewTap: UITapGestureRecognizer!
    
   
    @IBOutlet weak var parentView: UIView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!
    @IBOutlet weak var pickerView: UIPickerView!
    
    
    @IBAction func rootViewTapClick(_ sender: Any) {
        popVC();
        
    }
    
    @IBAction func cancelBtnClick(_ sender: Any) {
        popVC();
        
    }
    
    func pushAlertController() {
        let alertController = UIAlertController(title: "The system prompt",
                                                message: "Make sure the end is greater than the start", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: {
            action in
            
        })
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
    
    @IBAction func sureBtnClick(_ sender: Any) {
        
        switch type {
        case .switch1Item1?:
            cameraSixHus.sharedInstance.TLSwitch1Hour1 = UInt8(row0);
            cameraSixHus.sharedInstance.TLSwitch1Minute1 = UInt8(row1);
        case .switch1Item2?:
            cameraSixHus.sharedInstance.TLSwitch1Hour2 = UInt8(row0);
            cameraSixHus.sharedInstance.TLSwitch1Minute2 = UInt8(row1);
        case .switch2Item1?:
            cameraSixHus.sharedInstance.TLSwitch2Hour1 = UInt8(row0);
            cameraSixHus.sharedInstance.TLSwitch2Minute1 = UInt8(row1);
        case .switch2Item2?:
            cameraSixHus.sharedInstance.TLSwitch2Hour2 = UInt8(row0);
            cameraSixHus.sharedInstance.TLSwitch2Minute2 = UInt8(row1);
        case .switch3Item1?:
            cameraSixHus.sharedInstance.TLSwitch3Hour1 = UInt8(row0);
            cameraSixHus.sharedInstance.TLSwitch3Minute1 = UInt8(row1);
        case .switch3Item2?:
            cameraSixHus.sharedInstance.TLSwitch3Hour2 = UInt8(row0);
            cameraSixHus.sharedInstance.TLSwitch3Minute2 = UInt8(row1);
        default:
            break;
        }
        cameraSixHus.sharedInstance.timelapseSend();
       
        selectedStr = String(row0) + "H" + String(row1) + "M";
        postValueBlock!(selectedStr);
        popVC();
    }
    
    func popVC() {
        self.dismiss(animated: true, completion: nil);
    }
    
    func sureRow(){
        switch type {
        case .switch1Item1?:
            row0 = Int(cameraSixHus.sharedInstance.TLSwitch1Hour1);
            row1 = Int(cameraSixHus.sharedInstance.TLSwitch1Minute1);
        case .switch1Item2?:
            row0 = Int(cameraSixHus.sharedInstance.TLSwitch1Hour2);
            row1 = Int(cameraSixHus.sharedInstance.TLSwitch1Minute2);
        case .switch2Item1?:
            row0 = Int(cameraSixHus.sharedInstance.TLSwitch2Hour1);
            row1 = Int(cameraSixHus.sharedInstance.TLSwitch2Minute1);
        case .switch2Item2?:
            row0 = Int(cameraSixHus.sharedInstance.TLSwitch2Hour2);
            row1 = Int(cameraSixHus.sharedInstance.TLSwitch2Minute2);
        case .switch3Item1?:
            row0 = Int(cameraSixHus.sharedInstance.TLSwitch3Hour1);
            row1 = Int(cameraSixHus.sharedInstance.TLSwitch3Minute1);
        case .switch3Item2?:
            row0 = Int(cameraSixHus.sharedInstance.TLSwitch3Hour2);
            row1 = Int(cameraSixHus.sharedInstance.TLSwitch3Minute2);
        default: break
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView.dataSource = self as! UIPickerViewDataSource;
        pickerView.delegate = self as! UIPickerViewDelegate;
        
        leftLabel.text = "Hours";
        rightLabel.text = "Minutes";
        sureRow();
        
        pickerView.selectRow(row0+2400, inComponent: 0, animated: true);
        pickerView.selectRow(row1+6000, inComponent: 1, animated: true);
        
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        
        return 2;
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            if component == 0{
                return 4800;
            }else{
                return 12000;
            }
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            if component == 0{
                return String(row%24);
            }else{
                return String(row%60);
            }
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0{
            row0 = row%24;
        }else{
            row1 = row%60;
        }
       
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        
        if (touch.view?.isDescendant(of: parentView))! {
            return false;
        }else{
            return true;
            
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
    }
    /*     // MARK: - Navigation
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页