Click按钮如何关闭alertController并获取数据

时间:2016-12-26 06:58:11

标签: swift datetime time swift3

//在UIViewController中调用类

    label = TimePickerView()

    alertController = UIAlertController(title: " \n\n\n\n\n\n\n\n\n\n", message: "", preferredStyle: UIAlertControllerStyle.alert)

    alertController.view.addSubview(label)
   // alertController.view.addSubview(samplePicker)
    self.present(alertController, animated: true, completion: nil)

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

import UIKit

class TimePickerView: UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate {
    var hour:Int = 0
    var minute:Int = 0

    var  samplePicker: UIPickerView = UIPickerView()
    var sampleSegment:UISegmentedControl = UISegmentedControl ()
    var alertController:UIAlertController = UIAlertController()



    required internal init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.setup()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setup()

    }

    func setup(){

        self.delegate = self
        self.dataSource = self

        samplePicker = UIPickerView(frame: CGRect(x:10.0, y:40.0, width:10, height:150))
        samplePicker.delegate =  self
        samplePicker.dataSource = self
        samplePicker.showsSelectionIndicator = true
        samplePicker.tintColor =  UIColor.red
        samplePicker.reloadAllComponents()


        sampleSegment = UISegmentedControl(items: NSArray(object: "تایید") as [AnyObject])
        sampleSegment.isMomentary = true
        sampleSegment.frame = CGRect(x:0, y:0,width:270.0, height:30.0)
        sampleSegment.tintColor = UIColor.black
        sampleSegment.backgroundColor = UIColor.gray
        sampleSegment.addTarget(self, action: #selector(TimePickerView.dismissAlert), for: UIControlEvents.valueChanged)

        self.addSubview(sampleSegment)
        self.addSubview(samplePicker)




    }



    public func numberOfComponents(in pickerView: UIPickerView) -> Int {

        return 2

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0:
            self.hour = row
            print(row)
        case 1:
            self.minute = row
        default:
            print("No component with number \(component)")
        }
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return 24
        }

        return 60
    }

    private func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> Int {
        return 30
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {


        if component == 0 {

            return String(row)

        }else {

            return  String(row)
        }



    }
    func dismissAlert(){

        alertController.dismiss(animated: true, completion: nil)



    }

}

1 个答案:

答案 0 :(得分:1)

使用AddAction,UIAlertController的实例属性。