0

UIViewMenuBarというカスタムがあります。このメニューバーは、カスタムの「タブバー」で、内部にcollectionViewがあり、5つのセルがあります。セルをタップすると、私はViewControllerを提示したいが、私は現在の機能にアクセスすることはできませんので、私は私のdidSelect方法で以下のコードのようなものを呼び出すことはできません。UITabBarControllerのカスタムコレクションビューの使用

if indexPath.item == 1 { 
    let dummyController = DummyController() 
    present(dummyController, animated: true, completion: nil) 
} 

誰もが解決策を持っていますか?どんな助けも高く評価されます。君たちありがとう。私のコードは以下の通りです:

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

    lazy var collectionView: UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.backgroundColor = .clear 
     cv.delegate = self 
     cv.dataSource = self 
     return cv 
    }() 

    let seperatorView: UIView = { 
     let view = UIView() 
     view.backgroundColor = lightGray 
     return view 
    }() 

    let imageNames = ["home_selected", "glimpse_selected", "camera_selected", "activity_selected", "profile_selected"] 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     addSubview(collectionView) 
     addSubview(seperatorView) 

     collectionView.register(MenuCell.self, forCellWithReuseIdentifier: "cellId") 

     let selectedIndexPath = IndexPath(item: 0, section: 0) 
     collectionView.selectItem(at: selectedIndexPath, animated: false, scrollPosition: .centeredHorizontally) 

     _ = collectionView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0) 

     _ = seperatorView.anchor(collectionView.topAnchor, left: collectionView.leftAnchor, bottom: nil, right: collectionView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 1) 

     setupHorizontalBar() 
    } 

    var horizontalBarLeftAnchorConstraint: NSLayoutConstraint? 

    func setupHorizontalBar() { 
     let horizontalBarView = UIView() 
     horizontalBarView.translatesAutoresizingMaskIntoConstraints = false 
     horizontalBarView.backgroundColor = .darkGray 
     addSubview(horizontalBarView) 

     horizontalBarLeftAnchorConstraint = horizontalBarView.leftAnchor.constraint(equalTo: self.leftAnchor) 
     horizontalBarLeftAnchorConstraint?.isActive = true 

     horizontalBarView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true 

     horizontalBarView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1/5).isActive = true 
     horizontalBarView.heightAnchor.constraint(equalToConstant: 4).isActive = true 
    } 

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     let x = CGFloat(indexPath.item) * frame.width/5 
     horizontalBarLeftAnchorConstraint?.constant = x 

     UIView.animate(withDuration: 0.45, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
      self.layoutIfNeeded() 
     }, completion: nil) 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! MenuCell 

     cell.imageView.image = UIImage(named: imageNames[indexPath.item])?.withRenderingMode(.alwaysTemplate) 

     return cell 
    } 

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 

     return 0 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

答えて

0

は、カスタムビューのデリゲートを定義し、このビューを所有するView Controllerは、それを作成するとき、それはデリゲートとしての地位を設定し、プレゼンテーション自体を扱うことができます。

など。

メニューバーを作成し、あなたのビューコントローラで次に
protocol MenuBarDelegate: class { 
    func didSelectItem(index: IndexPath, menuBar: MenuBar) 
} 

class MenuBar { 
    weak var delegate: MenuBarDelegate? 
} 

は行います

menuBar.delegate = self 

はdidSelectItemメソッドを実装し、そこにそれを扱う - あなたは、本質的に、彼らが使用してAppleのユビキタスデリゲートパターンの独自のバージョンを作っていますすべての意見に選択した項目をindexPath以外のものとしてモデル化したいかもしれませんが、あなたのコードは分かりません。

モデル論理をビュー論理から常に切り離すことをお勧めします。たとえば、ビューには表示されているモデルが決してわからず、ユーザーがそのモデルとやり取りするときに何をすべきか分かりません。これは常に分離され、転送されるべきです。

+0

上記のコードで例を挙げてください。私は少し初心者です。申し訳ありませんが痛みです。あなたの答えをありがとう!心から感謝する。トップマン – Jimmy

+0

MenuBarファイルの最上部にプロトコルを追加し、そのプロパティをmenuBarに追加するだけです。次に、MenuBar自体を作成してビュー階層に追加する場合は、そのビューコントローラにデリゲートを設定し、プロトコル定義に必要な機能を実装します。 https://developer.apple.com/library/prerelease/content/documentation/General/Conceptual/DevPedia-CocoaCore/Delegationhtml https://developer.apple.com/library/content/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html Objective-C but relevant – Tim

0

モデル論理を常にビューロジックから分離する必要があるという点については、@ Jeffに同意します。しかし、私は最近、自分のタブバーを実装し、私はそれが次のように作られた:私はどこでもからアクセスできるように

  1. 私はAppDelegateでインスタンス化UINavigationControllerを持っていました。
  2. あなたのカスタムMenuBarでは、このナビゲーションコントローラにアクセスするには、(UIApplication.shared.delegate as AppDelegate).YOURNAVIGATIONCONTROLLERを呼び出して、それを使って提示したいビューコントローラを提示することができます。
  3. あなたが実際に、あなたは常にあなたが次のようにnavigationcontrollerのrootViewControllerとして提示したいViewControllerを設定する必要がありますUITabbarControllerとしてあなたMenuBarを使用したい場合は、次のよう

    VAR appDelegate = UIApplication.shared.delegateを? AppDelegate appDelegate?.YOURNAVIGATIONCONTROLLER?.viewControllers = [(DummyController())!]

希望に便利でした!

関連する問題