2017-01-18 15 views
0

プログラムでメニューを構築しましたが、各メニュー項目をストーリーボード内の別のviewControllerにリンクしたいので、UIを制御できます。これまでのところ、私は持っています:ViewControllerにメニューをリンクするにはどうすればいいですか?

import UIKit 

class Setting: NSObject { 

    let name: String 
    let imageName: String 

    init(name: String, imageName: String) { 
     self.name = name 
     self.imageName = imageName 

    } 
} 

class SettingsLauncher: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 
    @available(iOS 6.0, *) 
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return settings.count 
    } 

    let blackView = UIView() 

    let collectionView: UICollectionView = { 

     let layout = UICollectionViewFlowLayout() 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.backgroundColor = UIColor.white 
     return cv 

    }() 


    let cellId = "cellId" 
    let cellHeight: CGFloat = 40 

    let settings: [Setting] = { 

     return [Setting(name: "News", imageName: "news"), Setting(name: "Settings", imageName: "settings"), Setting(name: "UI list", imageName: "list"), Setting(name: "Book", imageName: "book"), Setting(name: "Me", imageName: "me"), Setting(name: "Cancel", imageName: "close")] 

    }() 

    func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect { 
     return CGRect(x: x, y: y, width: width, height: height) 
    } 

    var TableViewController: TableViewController? 


    func showSettings() { 

     if let window = UIApplication.shared.keyWindow { 

      blackView.backgroundColor = UIColor(white: 0, alpha: 0.5) 

      blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss))) 

      window.addSubview(blackView) 

      window.addSubview(collectionView) 

      let height: CGFloat = CGFloat(settings.count) * cellHeight 
      let y = window.frame.height - height 
      collectionView.frame = CGRectMake(0, window.frame.height, window.frame.width,height) 

      blackView.frame = window.frame 
      blackView.alpha = 0 

      UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 

       self.blackView.alpha = 1 

       self.collectionView.frame = self.CGRectMake(0, y, self.collectionView.frame.width, self.collectionView.frame.height) 
      }, completion: nil) 
     } 
    } 

    func handleDismiss() { 

     UIView.animate(withDuration: 0.5) { 
      self.blackView.alpha = 0 

      if let window = UIApplication.shared.keyWindow { 
       self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height) 

      } 

     } 
    } 



    //func numberOfSections(in collectionView: UICollectionView) -> Int { 
    // return 3 
    //} 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SettingCell 

     let setting = settings[indexPath.item] 
     cell.setting = setting 

     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width: collectionView.frame.size.width,height: cellHeight) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
     return 0 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     //print(setting.name) 
     // handleDismiss() 

     UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 

      self.blackView.alpha = 0 

      if let window = UIApplication.shared.keyWindow { 
       self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height) 

      } 

     }) { (completed: Bool) in 

      let setting = self.settings[indexPath.item] 
      if setting.name != "Cancel" { 
       self.TableViewController?.showControllerForSetting(setting: setting) 

      } 


     } 


    } 

    override init() { 
     super.init() 


     collectionView.dataSource = self 
     collectionView.delegate = self 

     collectionView.register(SettingCell.self, forCellWithReuseIdentifier: cellId) 

    } 

} 

メニューリストは、コードから見ることができます配列から取得されます。私はそれぞれ独自のviewControllerにリンクしたいと思います。私はこれについてオンラインのチュートリアルに従いました。このメニューシステムはビューの一番下からスライドして自分自身を明らかにします。

+0

に設定を渡す以下の方法

func goToNextViewController(setting: setting){ if setting.name == "News"{ //Here implement code to go to News screen }else if setting.name == "Settings"{ //Here implement code to go to Settings screen }else if setting.name == "UI list"{ //Here implement code to go to UIList screen }else if setting.name == "Book"{ //Here implement code to go to Book screen }else if setting.name == "Me"{ //Here implement code to go to Me screen }else{ self.TableViewController?.showControllerForSetting(setting: setting) } } 

を追加あなたのUIスクリーンショットを追加してください。 –

+0

メニューの意味は?またはどのUI?メニューの – Sole

+0

の出力。 –

答えて

0

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     //print(setting.name) 
     // handleDismiss() 

     UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 

      self.blackView.alpha = 0 

      if let window = UIApplication.shared.keyWindow { 
       self.collectionView.frame = self.CGRectMake(0, window.frame.height, self.collectionView.frame.width, self.collectionView.frame.height) 

      } 

     }) { (completed: Bool) in 

      let setting = self.settings[indexPath.item] 
      if setting.name != "Cancel" { 
       goToNextViewController(setting: setting) 

      } 


     } 


    } 

以下でごcollectionView(_didSelectItemAt :)を更新し、他のすべての画面についてすでに実装ロジックを持つshowControllerForSettingメソッドを持っている場合のみself.TableViewController?.showControllerForSetting(setting: setting)

+0

goToNextViewController関数の実装は、switch文を使用するときれいになります。 –

+0

もしそれ以外の場合は、彼がどのように目的地に到達できるかを論理的に明確にしても問題ありません。 –

+0

Zaidが必要なように見えます。コメントを追加した最初の画面のサンプルコードを表示できますか? – Sole

関連する問題