我创建了一个包含四个项目的导航菜单,现在我想用视图控制器链接每个项目,我该怎么做?
我使用过本教程:How to create navigation panel
CenterViewControllerDelegate :
@objc
protocol CenterViewControllerDelegate {
optional func toggleLeftPanel()
optional func collapseSidePanels()
}
class CenterViewController: UIViewController {
@IBOutlet weak private var imageView: UIImageView!
@IBOutlet weak private var titleLabel: UILabel!
@IBOutlet weak private var creatorLabel: UILabel!
var delegate: CenterViewControllerDelegate?
// MARK: Button actions
@IBAction func kittiesTapped(sender: AnyObject) {
delegate?.toggleLeftPanel?()
}
}
extension CenterViewController: SidePanelViewControllerDelegate {
func ItemMenuSelected(ItemMenu: Menu) {
imageView.image = ItemMenu.image
titleLabel.text = ItemMenu.title
creatorLabel.text = ItemMenu.creator
delegate?.collapseSidePanels?()
}
}
SidePanelViewControllerDelegate :
@objc
protocol SidePanelViewControllerDelegate {
func ItemMenuSelected(ItemMenu: Menu)
}
class SidePanelViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var delegate: SidePanelViewControllerDelegate?
var ItemMenus: Array<Menu>!
struct TableView {
struct CellIdentifiers {
static let MenuCell = "MenuCell"
}
}
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
override func viewDidLoad() {
super.viewDidLoad()
var tblView = UIView(frame: CGRectZero)
tableView.tableFooterView = tblView
tableView.tableFooterView!.hidden = true
tableView.backgroundColor = UIColorFromRGB(0xE2F2C9)
tableView.reloadData()
}
}
// MARK: Table View Data Source
extension SidePanelViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ItemMenus.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(TableView.CellIdentifiers.MenuCell, forIndexPath: indexPath) as! MenuCell
cell.configureForMenu(ItemMenus[indexPath.row])
return cell
}
}
// Mark: Table View Delegate
extension SidePanelViewController: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedMenu = ItemMenus[indexPath.row]
delegate?.ItemMenuSelected(selectedMenu)
}
}
class MenuCell: UITableViewCell {
@IBOutlet weak var ItemMenuImageView: UIImageView!
@IBOutlet weak var imageNameLabel: UILabel!
@IBOutlet weak var imageCreatorLabel: UILabel!
func configureForMenu(ItemMenu: Menu) {
ItemMenuImageView.image = ItemMenu.image
imageNameLabel.text = ItemMenu.title
imageCreatorLabel.text = ItemMenu.creator
}
}