FSCalendar 날짜를 탭하면 TextField가 있는 Alert를 표시합니다.

13317 단어 Swift

이런 느낌입니다





코드 및 간략한 설명


  • 먼저 표시 할 Alert를 만듭니다.
  • 경고에 TextField를 표시하려면 alert.addTextField(configurationHandler:((UITextField) -> Void)?)를 사용하십시오.

  • Alert
        func showTextFieldAlert(targetView:UIViewController){
    
            var productNameTextField:UITextField? //Alertに表示したTextField.Textを入れて使います
            var janCodeTextField:UITextField?
            var deadlineDayTextField:UITextField?
    
            let alert = UIAlertController(title: "データを追加", message:"データを追加する場合は、値を入力して下さい", preferredStyle: .alert)
    
            alert.addTextField { (alertProductNameTextField:UITextField) in
    
                alertProductNameTextField.placeholder = "プレスホルダー"
                productNameTextField = alertProductNameTextField) //
    
            }
    
            alert.addTextField { (alertJANCodeTextField:UITextField) in
    
                alertJANCodeTextField.placeholder = "プレスホルダー"
                janCodeTextField = alertJANCodeTextField
    
            }
    
            alert.addTextField { (alertDeadlineDayTextField:UITextField) in
    
                alertDeadlineDayTextField.placeholder = "プレスホルダー"
                deadlineDayTextField = alertDeadlineDayTextField
    
            }
    
            alert.addAction(UIAlertAction(title: "キャンセル", style: .default, handler: nil))
    
            alert.addAction(UIAlertAction(title: "追加", style: .default, handler: { _ in
    
                //追加が押された時の処理
    
             }))
    
            targetView.present(alert, animated: true, completion: nil)
    
        }
    
  • FSCalendar에서 날짜 탭을 감지하려면 func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {}를 사용하십시오.

  • FSCalendar
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    
               //カレンダーの日付がタップされた時に働く処理
    
         }
    

    실제로 앱으로 작성한 코드



    Alert



    Alert
    extension Alert{   
    
        func showTextFieldAlert(targetView:UIViewController){
    
            let realmCRUDModel = RealmCRUDModel()
    
            var productNameTextField:UITextField?
            var janCodeTextField:UITextField?
            var deadlineDayTextField:UITextField?
    
            let alert = UIAlertController(title: "データを追加", message:"データを追加する場合は、値を入力して下さい", preferredStyle: .alert)
    
            alert.addTextField { (alertProductNameTextField:UITextField) in
    
                alertProductNameTextField.placeholder = "商品名"
                productNameTextField = alertProductNameTextField
    
            }
    
            alert.addTextField { (alertJANCodeTextField:UITextField) in
    
                alertJANCodeTextField.placeholder = "JANコード"
                janCodeTextField = alertJANCodeTextField
    
            }
    
            alert.addTextField { (alertDeadlineDayTextField:UITextField) in
    
                alertDeadlineDayTextField.placeholder = "期限 例:2021年01月01日"
                deadlineDayTextField = alertDeadlineDayTextField
    
            }
    
            alert.addAction(UIAlertAction(title: "キャンセル", style: .default, handler: nil))
    
            alert.addAction(UIAlertAction(title: "追加", style: .default, handler: { _ in
    
                realmCRUDModel.createRealm(createProductName: (productNameTextField?.text)!, createJANCode: (janCodeTextField?.text)!, createDeadlineDay: (deadlineDayTextField?.text)!)
    
             }))
    
            targetView.present(alert, animated: true, completion: nil)
    
        }
    
    }
    

    FSCalendar



    ViewController
        func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
    
            alert.showTextFieldAlert(targetView: self)
    
         }
    



    처음 TextField가 포함된 Alert를 쓸 때
    Alert내의 TextField.text를 어떻게 하면 사용할 수 있는지 모르는 것이 그리운 

    좋은 웹페이지 즐겨찾기