Swift:使用CollectionViewCell Segue在CollectionView中构建菜单

时间:2016-06-24 13:45:02

标签: swift uiviewcontroller segue collectionview

我正在使用CollectionView构建一个菜单,并以编程方式尝试附加5个按钮。我无法弄清楚如何将按钮连接到segues。这些按钮激活容器视图,然后在该视图中切换内容。这是我到目前为止所拥有的MenuContainerViewController,它嵌入在MainMenuViewController中。任何帮助将不胜感激。

import UIKit

class MenuContainerViewController: UIViewController {

 var firstVC: MainMenuViewController?

enum SegueIdentifier: String {
    case SegueToTravelViewIdentifier = "SegueToTravelViewIdentifier"
    case SegueToHistoryViewIdentifier = "SegueToHistoryViewIdentifier"
    case SegueToLimitsViewIdentifier = "SegueToLimitsViewIdentifier"
    case SegueToBalanceViewIdentifier = "SegueToBalanceViewIdentifier"
    case SegueToEditViewIdentifier = "SegueToEditViewIdentifier"
    case SegueToHelpViewIdentifier = "SegueToHelpViewIdentifier"

    init?(optionalRawValue: String?) {
         if let value = optionalRawValue {
            switch value {
            case "SegueToTravelViewIdentifier": self = .SegueToTravelViewIdentifier
            case "SegueToHistoryViewIdentifier": self = .SegueToHistoryViewIdentifier
            case "SegueToLimitsViewIdentifier": self = .SegueToLimitsViewIdentifier
            case "SegueToBalanceViewIdentifier": self = .SegueToBalanceViewIdentifier
            case "SegueToEditViewIdentifier": self = .SegueToEditViewIdentifier
            case "SegueToHelpViewIdentifier": self = .SegueToHelpViewIdentifier
            default: return nil
            }
        }
        return nil
    }


}


var vc : UIViewController!

var segueIdentifier : String!

var lastViewController: UIViewController!

override func viewDidLoad() {
    super.viewDidLoad()
    segueIdentifierReceivedFromParent("mainButton")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func segueIdentifierReceivedFromParent(button: String){
    if button == "mainButton"
    {

        self.segueIdentifier = "main"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
    else if button == "travelButton"
    {
        self.segueIdentifier = "travel"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
    else if button == "balanceButton"
    {
        self.segueIdentifier = "balance"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
    else if button == "limitsButton"
    {
        self.segueIdentifier = "limits"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
    else if button == "historyButton"
    {
        self.segueIdentifier = "history"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
    else if button == "helpButton"
    {
        self.segueIdentifier = "help"
        self.performSegueWithIdentifier(self.segueIdentifier, sender: nil)
    }
}
/*
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == segueIdentifier{

        if lastViewController != nil{
            lastViewController.view.removeFromSuperview()

        }
        vc = segue.destinationViewController
        self.addChildViewController(vc)
        vc.view.frame = CGRect(x: 0,y: 0, width: self.view.frame.width,height: self.view.frame.height)
        self.view.addSubview(vc.view)

        vc.didMoveToParentViewController(self)
        lastViewController = vc

    }

}
*/
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let segueIdentifierValue = SegueIdentifier(rawValue: segue.identifier!) {

        if lastViewController != nil {
            lastViewController.view.removeFromSuperview()
        }

        vc = segue.destinationViewController
        self.addChildViewController(vc)

        vc.didMoveToParentViewController(self)
        lastViewController = vc

        switch segueIdentifierValue {
        case .SegueToTravelViewIdentifier:
            print("travel")
        case .SegueToHistoryViewIdentifier:
            print("history")
        case .SegueToLimitsViewIdentifier:
            print("limits")
        case .SegueToBalanceViewIdentifier:
            print("balance")
        case .SegueToEditViewIdentifier:
            print("edit")
        case .SegueToHelpViewIdentifier:
            print("help")
        }
    }
}

}

这是MainMenuViewController:

class MainMenuViewController: UIViewController,  UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {




//Container View
@IBOutlet weak var contain: UIView!

//Slide Menu Collection View
let reuseIdentifier = "cell"

var menus = Array<String>()

var containerView: MenuContainerViewController?

@IBOutlet weak var collectionView: UICollectionView!

@IBAction func menuButtonPressed(sender: AnyObject) {
    self.performSegueWithIdentifier("travel", sender: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()

    //Button names
    menus = ["button_instore", "button_atm", "button_online", "button_travel", "button_balance", "button_limits", "button_history", "button_edit", "button_help"]


}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func travel(sender: AnyObject) {
    containerView!.segueIdentifierReceivedFromParent("travelButton")
}

@IBAction func balance(sender: AnyObject) {
    containerView!.segueIdentifierReceivedFromParent("balanceButton")
}

@IBAction func limits(sender: AnyObject) {
    containerView!.segueIdentifierReceivedFromParent("limitsButton")
}
@IBAction func history(sender: AnyObject) {
    containerView!.segueIdentifierReceivedFromParent("historyButton")
}
@IBAction func help(sender: AnyObject) {
    containerView!.segueIdentifierReceivedFromParent("helpButton")
}
@IBAction func main(sender: AnyObject) {
    containerView!.segueIdentifierReceivedFromParent("mainButton")
}

/*override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "container"{

        containerView = segue.destinationViewController as? MenuContainerViewController


    }
}

* /

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.navigationController?.navigationBar.translucent = true

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.menus.count
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}



/*
func MenuContainerViewController(sender : UIButton) {

    let selectedButtonCell = sender.superview as! UICollectionViewCell
    let indexPath = collectionView.indexPathForCell(selectedButtonCell)
    if indexPath!.row == 0 {

    }

}

*/
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell

    //cell.menuLabel.text = self.menus[indexPath.item]
    cell.menuButton?.image = UIImage(named: menus[indexPath.row])
    cell.menuButtonPressed.tag = indexPath.row
    //cell.menuButtonPressed.addTarget(self, action: #selector(MenuContainerViewController), forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    print("You selected cell #\(indexPath.item)!")
}

}

0 个答案:

没有答案