bugfix> ios > 投稿

ユーザーが予約できるようにUDatePickerを作成したいと思います。 UIDatePickerは、現在の日付の7日先を表示する必要があり、予約が行われると、確認が表示されて通知がポップアップ表示されます。

UIは重要ではなく、基本的なもので構いません。それは可能ですか?私はオンラインで検索してきましたが、役立つものは何も見つかりませんでした。

任意の助けをいただければ幸いです。

回答 1 件
  • class ViewController: UIViewController , UIPickerViewDelegate, 
    UIPickerViewDataSource
    {
    var pickerView: UIPickerView!
    @IBOutlet weak var textField: UITextField!
    var arrDate = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    
        arrDate.append(Date().getDateStringWithFormate("yyyy-MM-dd", timezone: TimeZone.current.abbreviation()!))
        for i in 1...7
        {
            let dateAdded = Date().addDays(i)
            arrDate.append(dateAdded.getDateStringWithFormate("yyyy-MM-dd", timezone: TimeZone.current.abbreviation()!))
            print(arrDate)
        }
    
        pickerView = UIPickerView()
        pickerView.delegate = self
        pickerView.dataSource = self
    
        let keyboardToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
        keyboardToolbar.barStyle = .black
        keyboardToolbar.tintColor = UIColor.white
        let barButtonItem = UIBarButtonItem(title: NSLocalizedString("Done", comment: ""), style: .done, target: self, action: #selector(clickToKeyboardToolbarDone(sender:)))
        //(barButtonSystemItem: .done, target: self, action: #selector(clickToKeyboardToolbarDone(sender:)))
    
        let flex = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let items = [flex, barButtonItem]
        keyboardToolbar.setItems(items, animated: true)
        textField.inputAccessoryView = keyboardToolbar
        textField.inputView = pickerView
    }
    @IBAction func clickToKeyboardToolbarDone(sender: UIBarButtonItem)
    {
        if(textField.isFirstResponder)
        {
            let selectedIndex = pickerView.selectedRow(inComponent: 0)
            let currentDate = arrDate[selectedIndex]
            print("currentDate",currentDate)
        }
        textField.resignFirstResponder()
    }
    //MARK: - UIPickerViewDelegate, UIPickerViewDataSource Methods
    func numberOfComponents(in pickerView: UIPickerView) -> Int
    {
        return 1
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return arrDate.count
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
    {
        return arrDate[row]
    }
    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.
    }
    */
    }
    
    //MARK: - All Date Extension
    extension Date
    {
       func getDateStringWithFormate(_ formate: String, timezone: String) -> String
        {
            let formatter = DateFormatter()
        formatter.dateFormat = formate
        formatter.timeZone = TimeZone(abbreviation: timezone)
        return formatter.string(from: self)
        }
    }
    
    

    customepickerビューを使用して、今日までの日付の配列を最大7日間作成し、このコードを試してください。

あなたの答え