swift - Click Button如何关闭alertController并获取数据

标签 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)



    }

}

最佳答案

使用 AddAction,UIAlertController 的一个实例属性。

关于swift - Click Button如何关闭alertController并获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327550/

相关文章:

Swift:协议(protocol)变量的反射

c# - 将字符串转换为 12 小时格式的日期时间格式

ruby-on-rails - Ruby:计算2次之间的时间差

php - PHP 时间戳计算的奇怪结果

php - 简单问题 : How to split date (08-17-2011) into month, 年、日? PHP

swift - 如何快速对数组中的不同结构模型进行排序?

swift - 表格 View 中的部分名称

ios - 在选择 CollectionViewCell 时将信息发送到详细 View

Symfony 日期时间字段,日和月可选

python - Django 中意外的日期时间格式