使用导航控制器缺少从第一个视图控制器到第二个视图控制器的过渡
Missing transition from first view controller to second view controller using navigation controller
我想创建一个导航层次结构,我想在其中使用 NavigationController 从 FirstViewController 转到 SecondViewController。 FirstViewController 包含一个按钮 B,我打算使用它转到 SecondViewController。我正在使用导航控制器概念,因为稍后我想 return 到 FirstViewController。我已经完成了以下步骤来实现它:
- 我已将 NavigationController 嵌入到包含 FirstViewController 和 SecondViewController 的故事板 X 中。
- 我创建了一个从 FirstView 中的按钮 B(具有与之关联的 FirstViewController)到 SecondView(具有 SecondViewController)的转接。
- 在呈现 firstViewController 后,我将 navigationViewController nvc 设置如下:
nvc = UINavigationController.init(rootViewController: firstViewController)
- 稍后,当按下按钮 B 时,我执行以下代码将 secondViewController 推送到导航层次结构中:
self.nvc?.pushViewController(secondViewController, animated: true)
但是,即使在推送之后,secondView 也不会出现。
- 我将按钮 B 和 secondViewController 之间的 segue 命名为 kSegue,因此我尝试执行 segue 作为附加项以查看 secondViewController 是否出现:
self.performSegue(withIdentifier: "kSegue", sender: self)
第4步和第5步同时执行时出现异常。异常表明我正在推送导航层次结构中已经存在的 viewController,但即使我注释了 performSegue 代码,第二个视图仍然无法打开。
请问我这里犯了什么错误?
在情节提要中,确保从导航控制器到第一个视图控制器有一个 rootViewController segue。另外,确保导航控制器被标记为初始视图控制器。
在代码中,更改
self.nvc?.pushViewController...
到
self.navigationController?.pushViewController...
1) Take a navigation controller on your storyboard
2) Set navigation controller as initial view controller
3) set view controller A as a root view controller of your navigation controller
4) in "GoToB" method access View controller B's instance and push it in navigation controller
5) On View controller B's "Go Back" method write code to pop it.
6) Dont forget to set storyboard Id on both A & B view controller
class FirstViewController: UIViewController
{
lazy var secondViewController : SecondViewController? =
{
let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
return secondViewController
}()
override func viewDidLoad()
{
super.viewDidLoad()
}
@IBAction func goToB(sender : UIButton)
{
guard let secondViewController = self.secondViewController else
{
return
}
self.navigationController?.pushViewController(secondViewController, animated: true)
}
}
class SecondViewController: UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func goBack(sender : UIButton)
{
self.navigationController?.popViewController(animated: true)
}
}
我想创建一个导航层次结构,我想在其中使用 NavigationController 从 FirstViewController 转到 SecondViewController。 FirstViewController 包含一个按钮 B,我打算使用它转到 SecondViewController。我正在使用导航控制器概念,因为稍后我想 return 到 FirstViewController。我已经完成了以下步骤来实现它:
- 我已将 NavigationController 嵌入到包含 FirstViewController 和 SecondViewController 的故事板 X 中。
- 我创建了一个从 FirstView 中的按钮 B(具有与之关联的 FirstViewController)到 SecondView(具有 SecondViewController)的转接。
- 在呈现 firstViewController 后,我将 navigationViewController nvc 设置如下:
nvc = UINavigationController.init(rootViewController: firstViewController)
- 稍后,当按下按钮 B 时,我执行以下代码将 secondViewController 推送到导航层次结构中:
self.nvc?.pushViewController(secondViewController, animated: true)
但是,即使在推送之后,secondView 也不会出现。
- 我将按钮 B 和 secondViewController 之间的 segue 命名为 kSegue,因此我尝试执行 segue 作为附加项以查看 secondViewController 是否出现:
self.performSegue(withIdentifier: "kSegue", sender: self)
第4步和第5步同时执行时出现异常。异常表明我正在推送导航层次结构中已经存在的 viewController,但即使我注释了 performSegue 代码,第二个视图仍然无法打开。
请问我这里犯了什么错误?
在情节提要中,确保从导航控制器到第一个视图控制器有一个 rootViewController segue。另外,确保导航控制器被标记为初始视图控制器。
在代码中,更改
self.nvc?.pushViewController...
到
self.navigationController?.pushViewController...
1) Take a navigation controller on your storyboard
2) Set navigation controller as initial view controller
3) set view controller A as a root view controller of your navigation controller
4) in "GoToB" method access View controller B's instance and push it in navigation controller
5) On View controller B's "Go Back" method write code to pop it.
6) Dont forget to set storyboard Id on both A & B view controller
class FirstViewController: UIViewController
{
lazy var secondViewController : SecondViewController? =
{
let secondViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController
return secondViewController
}()
override func viewDidLoad()
{
super.viewDidLoad()
}
@IBAction func goToB(sender : UIButton)
{
guard let secondViewController = self.secondViewController else
{
return
}
self.navigationController?.pushViewController(secondViewController, animated: true)
}
}
class SecondViewController: UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func goBack(sender : UIButton)
{
self.navigationController?.popViewController(animated: true)
}
}