如何启用 SideMenu 使用 ViewController 功能 | Swift/Xcode
How to enable SideMenu to work with ViewController functions | Swift/Xcode
我正在使用名为“SideMenu”的 pod,它使用 tableView 创建菜单。菜单嵌入在我的 ViewController 中,但它无法访问 ViewController 的功能。当我点击菜单中的一行时,我希望它转到正确的屏幕。不幸的是,segues 源自 ViewController 而不是菜单。我显然不能使用 Delegate,因为我从不使用 tableView - 它是嵌入式的。我该如何解决这个问题?
class ViewController: UIViewController, UINavigationControllerDelegate {
var menu: SideMenuNavigationController?
override func viewDidLoad() {
super.viewDidLoad()
// Setup the menu
setupMenu()
}
func setupMenu() {
menu = SideMenuNavigationController(rootViewController: MenuListController())
menu?.setNavigationBarHidden(true, animated: false)
menu?.presentationStyle = .menuDissolveIn
SideMenuManager.default.rightMenuNavigationController = menu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
}
func showProfile() {
performSegue(withIdentifier: "profileSegue", sender: self)
}
func showHelp() {
performSegue(withIdentifier: "helpSegue", sender: self)
}
func showSettings() {
performSegue(withIdentifier: "settingsSegue", sender: self)
}
func showPreferences() {
performSegue(withIdentifier: "preferencesSegue", sender: self)
}
}
class MenuListController: UITableViewController {
var items = ["Profile", "Help", "Settings", "Preferences", "Log Out"]
var darkColor = UIColor(red: 33/255.0, green: 33/255.0, blue: 33/255.0, alpha: 1)
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = darkColor
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var content = cell.defaultContentConfiguration()
content.text = items[indexPath.row]
content.textProperties.color = .white
cell.contentConfiguration = content
cell.backgroundColor = darkColor
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 {
// Profile
} else if indexPath.row == 1 {
// Help
} else if indexPath.row == 2 {
// Settings
} else if indexPath.row == 3 {
// Preferences
} else if indexPath.row == 4 {
// Log Out
logOut()
}
}
}
我在 pod 的 GitHub 文档中找不到答案。无论如何,这是 link:
SideMenu
您也可以这样导航。
func openSettings(){
let storyBoard = UIStoryboard(name: "ReplaceStoryBoardName", bundle: nil)
let childVc = storyBoard.instantiateViewController(withIdentifier: "ReplaceYourIdentifierFromStoryBoard") as? YourViewControllerName
childVc.anyData = self.Data // or whatever data you want to sent you can inject it here.
self.navigationController?.pushViewController(childVc, animated: true)
}
还有一些其他方式,例如 NotificationCenter
您可以 post 来自 Sidemenu View Controller
的通知,然后在您的 ViewController
上观察它,您已经在其中嵌入了 SideMenu
我正在使用名为“SideMenu”的 pod,它使用 tableView 创建菜单。菜单嵌入在我的 ViewController 中,但它无法访问 ViewController 的功能。当我点击菜单中的一行时,我希望它转到正确的屏幕。不幸的是,segues 源自 ViewController 而不是菜单。我显然不能使用 Delegate,因为我从不使用 tableView - 它是嵌入式的。我该如何解决这个问题?
class ViewController: UIViewController, UINavigationControllerDelegate {
var menu: SideMenuNavigationController?
override func viewDidLoad() {
super.viewDidLoad()
// Setup the menu
setupMenu()
}
func setupMenu() {
menu = SideMenuNavigationController(rootViewController: MenuListController())
menu?.setNavigationBarHidden(true, animated: false)
menu?.presentationStyle = .menuDissolveIn
SideMenuManager.default.rightMenuNavigationController = menu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
}
func showProfile() {
performSegue(withIdentifier: "profileSegue", sender: self)
}
func showHelp() {
performSegue(withIdentifier: "helpSegue", sender: self)
}
func showSettings() {
performSegue(withIdentifier: "settingsSegue", sender: self)
}
func showPreferences() {
performSegue(withIdentifier: "preferencesSegue", sender: self)
}
}
class MenuListController: UITableViewController {
var items = ["Profile", "Help", "Settings", "Preferences", "Log Out"]
var darkColor = UIColor(red: 33/255.0, green: 33/255.0, blue: 33/255.0, alpha: 1)
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = darkColor
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var content = cell.defaultContentConfiguration()
content.text = items[indexPath.row]
content.textProperties.color = .white
cell.contentConfiguration = content
cell.backgroundColor = darkColor
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath.row == 0 {
// Profile
} else if indexPath.row == 1 {
// Help
} else if indexPath.row == 2 {
// Settings
} else if indexPath.row == 3 {
// Preferences
} else if indexPath.row == 4 {
// Log Out
logOut()
}
}
}
我在 pod 的 GitHub 文档中找不到答案。无论如何,这是 link: SideMenu
您也可以这样导航。
func openSettings(){
let storyBoard = UIStoryboard(name: "ReplaceStoryBoardName", bundle: nil)
let childVc = storyBoard.instantiateViewController(withIdentifier: "ReplaceYourIdentifierFromStoryBoard") as? YourViewControllerName
childVc.anyData = self.Data // or whatever data you want to sent you can inject it here.
self.navigationController?.pushViewController(childVc, animated: true)
}
还有一些其他方式,例如 NotificationCenter
您可以 post 来自 Sidemenu View Controller
的通知,然后在您的 ViewController
上观察它,您已经在其中嵌入了 SideMenu