2017-12-26 19 views
-2

私は、ユーザーが任意の日付を選択すると、別のviewControllerにリダイレクトされるカスタムカレンダーを作成するIOSを初めて使いました。デリゲートを使用して別のView Controllerを開く?

私はカレンダーを作成するには、このリンクを使用: https://github.com/Akhilendra/calenderAppiOS

私はデリゲートでそれを行っているが、私は私が間違って何をしたかを把握することはできません。

マイコード:

protocol SelectedDateDelagate: class { 
    func openAppointmentDetails() 
} 
class CalenderView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, MonthViewDelegate { 

    weak var delegate: SelectedDateDelagate? 
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let cell=collectionView.cellForItem(at: indexPath) 
     cell?.backgroundColor=Colors.darkRed 
     let lbl = cell?.subviews[1] as! UILabel 
     lbl.textColor=UIColor.yellow 

     let calcDate = indexPath.row-firstWeekDayOfMonth+2 

     print("u selected date",calcDate,monthName) 

     delegate?.openAppointmentDetails() 
    } 
} 


class ViewController: UIViewController,SelectedDateDelagate { 
    func openAppointmentDetails() { 

     let myVC = storyboard?.instantiateViewController(withIdentifier: "appointmentDetailsVC") as! AppointmentDetailsViewController 

     navigationController?.pushViewController(myVC, animated: true) 

    } 
} 

私は何もやることが起こるの日付をクリックしたとき、今の問題があります。

+0

あなたはこれをデバッグしましたか?それをナビゲーションコントローラにプッシュする前に、 'myVC'の値は何ですか?また、 'delegate'は' openAppointmentDetails() 'を呼び出す前に設定されていますか? – halileohalilei

答えて

0

を。私は変更があり、ここで

)のViewController

のcalenderView.delegate =のviewDidLoadで自己 ()メソッドを追加するのを忘れ、また、(openAppointmentDetailsに変更されます。

class ViewController: UIViewController,SelectedDateDelagate { 
func openAppointmentDetails() { 

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "appointmentDetailsVC") as! AppointmentDetailsViewController 
    self.present(nextViewController, animated:true, completion:nil) 

} 

override func viewDidLoad() { 
    calenderView.delegate = self 
} 

}

0

この場合はストーリーボードを使用し、コントロールビューで2つ目のビューコントローラーにコントロールビューをドラッグすることをお勧めします。ビューコントローラを提示する以外の目的でデリゲートを使用しないので、それを取り除くだけでよいでしょう。

0

他のケースでは、代理人が使用されます。この状況では、それは必要ではありません。さらに、MVCと矛盾するので、UIViewをDelegatesおよびDataSourceに準拠させる必要はありません。代わりに、UIViewController内にカレンダービュー(コレクションビュー)を配置し、メソッドを実装するためにこのコントローラーを適合させます。

あなたはこれを行う必要があります:私はこれを解決し

class CalenderViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

var calenderView: UICollectionView! 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = collectionView.cellForItem(at: indexPath)! 
    cell.backgroundColor = Colors.darkRed 
    let lbl = cell.subviews[1] as! UILabel 
    lbl.textColor = UIColor.yellow 

    let calcDate = indexPath.row - firstWeekDayOfMonth + 2 

    openAppointmentDetails() 
} 

func openAppointmentDetails() { 
    let appointmentVC = storyboard?.instantiateViewController(withIdentifier: "appointmentDetailsVC") as! AppointmentDetailsViewController 
    navigationController?.pushViewController(appointmentVC, animated: true) 
} 
} 
関連する問題