UIDatePicker没有出现在UITextField中。斯威夫特3

时间:2017-07-05 05:11:10

标签: ios swift swift3 uitextfield uidatepicker

我在1个视图控制器中有5个UITextField。前4个显示一个弹出的UIPickerView,其中填充了来自相应数组的数据。当我旋转拾取器时,该部分将显示在文本字段中。

第五个UITextField显示一个弹出式UIDatepicker,但日期和时间从不显示在文本字段中。

我已经观看了无数的教程,并在这个论坛上搜索了几天,但仍然无法使其发挥作用。

我做错了什么?任何帮助将不胜感激。

这是我的代码:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    var pickerView1 = UIPickerView()
    var pickerView2 = UIPickerView()
    var pickerView3 = UIPickerView()
    var pickerView4 = UIPickerView()
    var pickerView5 = UIDatePicker()

    @IBOutlet var mountainTextField: UITextField!
    @IBOutlet var difficultyTextField: UITextField!
    @IBOutlet var runTextField: UITextField!
    @IBOutlet var whenTextField: UITextField!
    @IBOutlet var snowTextField: UITextField!
    @IBAction func whenTextField(_ sender: UITextField) {        
        let datePickerView:UIDatePicker = UIDatePicker()
        datePickerView.datePickerMode = UIDatePickerMode.dateAndTime
        sender.inputView = datePickerView
        datePickerView.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControlEvents.valueChanged)
    }

    var mountain = ["A", "B"]
    var difficulty = ["1", "2", "3", "4"]
    var run = ["5", "6", "7", "8"]
    var snow = ["C", "D", "E", "F"]

    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView1 = UIPickerView()
        pickerView2 = UIPickerView()
        pickerView3 = UIPickerView()
        pickerView4 = UIPickerView()
        pickerView5 = UIDatePicker()
        pickerView1.delegate = self
        pickerView2.delegate = self
        pickerView3.delegate = self
        pickerView4.delegate = self

        pickerView1.tag = 0
        pickerView2.tag = 1
        pickerView3.tag = 2
        pickerView4.tag = 3
        pickerView5.tag = 4

        self.mountainTextField.inputView = self.pickerView1;
        self.difficultyTextField.inputView = self.pickerView2;
        self.runTextField.inputView = self.pickerView3;
        self.snowTextField.inputView = self.pickerView4;
        self.whenTextField.inputView = self.pickerView5;
    }

    func datePickerValueChanged(sender:UIDatePicker) {
        let dateFormatter = DateFormatter()

        whenTextField.text = dateFormatter.string(from: sender.date)
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView.tag == 0 {
            return mountain.count
        } else if pickerView.tag == 1 {
            return difficulty.count
        } else if pickerView.tag == 2 {
            return  run.count
        } else if  pickerView.tag == 3 {
            return snow.count
        }

        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView.tag == 0 {
            return mountain[row]
        } else if pickerView.tag == 1 {
            return difficulty[row]
        } else if pickerView.tag == 2 {
            return run[row]
        } else if pickerView.tag == 3 {
            return snow[row]
        }

        return ""
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {
        if pickerView.tag == 0 {
            mountainTextField.text = mountain[row]
        } else if pickerView.tag == 1 {
            difficultyTextField.text = difficulty[row]
        } else if pickerView.tag == 2 {
            runTextField.text = run[row]
        } else if pickerView.tag == 3 {
            snowTextField.text = snow[row]
        }
    }
}

6 个答案:

答案 0 :(得分:1)

https://github.com/yudiz-solutions/PickerDemo 请尝试这个库,它提供所有选择器和易于实现回调

答案 1 :(得分:0)

乍一看,您似乎没有为pickerView5设置委托?

答案 2 :(得分:0)

我相信你必须在将date转换为string时为DateFormatter提供dateFormat。 尝试使用

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:theme="@style/ThemeOverlay.AppCompat.Dark"
            app:titleTextColor="@android:color/white"
            app:contentInsetStartWithNavigation="0dp">
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/layout_scorll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp">

                <TextView
                    android:text="Price"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="start|bottom"
                    android:textStyle="bold"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginTop="20dp" />

                <TextView
                    android:id="@+id/seekbar_value"
                    android:text="Any"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center|bottom"
                    android:textStyle="bold"/>

               <org.florescu.android.rangeseekbar.RangeSeekBar
                    android:id="@+id/range_seekbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:showLabels="false"
                    app:valuesAboveThumbs="false"
                    app:barHeight="5dp"
                    app:alwaysActive="true"
                    app:thumbDisabled="@drawable/seekbar_button"
                    app:textAboveThumbsColor="@color/colorPrimary"
                    app:defaultColor="@color/colorPrimary"
                    app:activeColor="@color/colorButton" />

                <TextView
                    android:text="Block"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="start|bottom"
                    android:textStyle="bold"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginTop="20dp"/>

                <GridView
                    android:id="@+id/gridViewBlock"
                    android:numColumns="auto_fit"
                    android:gravity="center"
                    android:columnWidth="60dp"
                    android:stretchMode="columnWidth"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:nestedScrollingEnabled="false"/>

                <TextView
                    android:text="Floor"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="start|bottom"
                    android:textStyle="bold"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginTop="20dp"/>

                <!--  This Grid View is my problem -->
                <GridView
                    android:id="@+id/gridViewFloor"
                    android:numColumns="auto_fit"
                    android:gravity="center"
                    android:columnWidth="60dp"
                    android:stretchMode="columnWidth"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:nestedScrollingEnabled="false"/>

            </LinearLayout>

            <Button
                android:text="See result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:textStyle="bold"
                android:background="@color/colorAccent"
                android:textColor="@android:color/white" />

        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

答案 3 :(得分:0)

UIDatePicker不需要在swift 3中添加委托只需检查代码

class ViewController: UIViewController {
//Text Field Connection
@IBOutlet weak var txtDatePicker: UITextField!
//Uidate picker
let datePicker = UIDatePicker()

override func viewDidLoad() {
    super.viewDidLoad()
    //show date picker
    showDatePicker()
}


func showDatePicker(){
    //Formate Date
    datePicker.datePickerMode = .date

    //ToolBar
    let toolbar = UIToolbar();
    toolbar.sizeToFit()

    //done button & cancel button
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "cancelDatePicker")
    toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)

    // add toolbar to textField
    txtDatePicker.inputAccessoryView = toolbar
    // add datepicker to textField
    txtDatePicker.inputView = datePicker

}

func donedatePicker(){
    //For date formate
    let formatter = DateFormatter()
    formatter.dateFormat = "dd/MM/yyyy"
    txtDatePicker.text = formatter.string(from: datePicker.date)
    //dismiss date picker dialog
    self.view.endEditing(true)
}

  func cancelDatePicker(){
    //cancel button dismiss datepicker dialog
    self.view.endEditing(true)
  }
}

检查完整源代码到github链接:https://github.com/enamul95/DatePicker1

答案 4 :(得分:0)

viewDidload

中添加以下行
   pickerView5.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControlEvents.valueChanged)

并将下面的datePickerValueChanged替换为

 func datePickerValueChanged(sender:UIDatePicker) {

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"   // or whatever date format you want
    whenTextField.text = dateFormatter.string(from: sender.date)
}

那就是它!

答案 5 :(得分:0)

您的代码有两处变化,低于......

<强> 1

pickerView5.addTarget(self, action: #selector(datePickerValueChanged(sender:)), for: .valueChanged)

2。

func datePickerValueChanged(sender:UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    whenTextField.text = dateFormatter.string(from: sender.date)
}

完整运行代码

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

    var pickerView1 = UIPickerView()
    var pickerView2 = UIPickerView()
    var pickerView3 = UIPickerView()
    var pickerView4 = UIPickerView()
    var pickerView5 = UIDatePicker()


    @IBOutlet var mountainTextField: UITextField!
    @IBOutlet var difficultyTextField: UITextField!
    @IBOutlet var runTextField: UITextField!
    @IBOutlet var whenTextField: UITextField!
    @IBOutlet var snowTextField: UITextField!

//    @IBAction func whenTextField(_ sender: UITextField) {
//        
//        let datePickerView:UIDatePicker = UIDatePicker()
//        datePickerView.datePickerMode = UIDatePickerMode.dateAndTime
//        sender.inputView = datePickerView
//        datePickerView.addTarget(self, action: #selector(ViewController.datePickerValueChanged), for: UIControlEvents.valueChanged)
//    }



    var mountain = ["A", "B"]
    var difficulty = ["1", "2", "3", "4"]
    var run = ["5", "6", "7", "8"]
    var snow = ["C", "D", "E", "F"]


    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView1 = UIPickerView()
        pickerView2 = UIPickerView()
        pickerView3 = UIPickerView()
        pickerView4 = UIPickerView()
        pickerView5 = UIDatePicker()

        pickerView1.delegate = self
        pickerView2.delegate = self
        pickerView3.delegate = self
        pickerView4.delegate = self

        pickerView1.tag = 0
        pickerView2.tag = 1
        pickerView3.tag = 2
        pickerView4.tag = 3
        pickerView5.tag = 4

        self.mountainTextField.inputView = self.pickerView1;
        self.difficultyTextField.inputView = self.pickerView2;
        self.runTextField.inputView = self.pickerView3;
        self.snowTextField.inputView = self.pickerView4;
        self.whenTextField.inputView = self.pickerView5;

        pickerView5.addTarget(self, action: #selector(datePickerValueChanged(sender:)), for: .valueChanged)
    }



    func datePickerValueChanged(sender:UIDatePicker) {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        whenTextField.text = dateFormatter.string(from: sender.date)
    }


    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if pickerView.tag == 0 {
            return mountain.count
        } else if pickerView.tag == 1 {
            return difficulty.count
        } else if pickerView.tag == 2 {
            return  run.count
        } else if  pickerView.tag == 3 {
            return snow.count
        }
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        if pickerView.tag == 0 {
            return mountain[row]
        } else if pickerView.tag == 1 {
            return difficulty[row]
        } else if pickerView.tag == 2 {
            return run[row]
        } else if pickerView.tag == 3 {
            return snow[row]
        }

        return ""
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {

        if pickerView.tag == 0 {
            mountainTextField.text = mountain[row]
        } else if pickerView.tag == 1 {
            difficultyTextField.text = difficulty[row]
        } else if pickerView.tag == 2 {
            runTextField.text = run[row]
        } else if pickerView.tag == 3 {
            snowTextField.text = snow[row]
        }
    }
}