如何将菜单链接到ViewControllers?

时间:2017-01-18 12:18:43

标签: ios swift swift3

我有一个以编程方式构建的菜单,我希望每个菜单项链接到storyboard中的不同viewControllers,因此我可以控制UI。到目前为止,我有:

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,我确实遵循了一个在线教程,这个菜单系统从视图的底部向上滑动以显示自己。

1 个答案:

答案 0 :(得分:0)

使用以下

更新您的rich:dataList id="dtList" rows="30" value="#{SegundaVia.listInsc}" var="list" a4j:commandLink onclick="s2(#{list.inscnum});" oncomplete="openConfirmInsc();" action="#{getmanaged$FichaInscricaoSessionBean().teste}" value="#{list.contcargnum.contnum.userorgao} - #{list.inscnum} - #{list.contcargnum.cargnum.cargdescr}" rich:dataList>
collectionView(_didSelectItemAt :)

并添加以下方法

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)

            }


        }


    }

如果你有showControllerForSetting方法已经为所有其他屏幕实现了逻辑,那么只传递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) } } 中的设置