let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: Selector(("HomeTabController")))
self.navigationItem.leftBarButtonItem = backButton
上面的代码是在navigationController
中创建按钮的,但是我可以创建一个无法编写要打开的特定视图控制器的按钮。
我尝试使用popViewController
和popToRootViewController
进行操作,需要特定的代码来借助特定的viewController
故事板ID和快速打开特定的viewController
viewcontrollername。
答案 0 :(得分:0)
您需要在#selctor()
(Swift 4版本)中添加功能名称
let backButton = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(goToViewController(_:)))
self.navigationItem.leftBarButtonItem = backButton
您需要创建一个功能。
@objc func goToViewController(_ sender: UIBarButtonItem) {
//write code here to open a view controller
let storyboard = "Main"
let viewControllerIdentifier = "HomeVC"
let viewController = UIStoryboard(name: storyboard, bundle: nil).instantiateViewController(withIdentifier: viewControllerIdentifier) as! HomeVC
//push/present "viewController"
}
答案 1 :(得分:0)
在此代码中,只需将ChatVC名称替换为您的视图控制器名称
@objc func goToViewController(_ sender: UIBarButtonItem) {
for controller in self.navigationController!.viewControllers as Array {
if controller.isKind(of: ChatVC.self) {
self.navigationController!.popToViewController(controller, animated: true)
break
} else {
self.navigationController?.popViewController(animated: true)
}
}
}