我试图在不设计 Storyboard的情况下将 View Controller 推送到导航 Controller 。这可能吗,因为我是 Swift 的新手?我在 appdelegate.swift 文件中创建了我的导航 Controller :
let viewController: UIViewController = ViewController(nibName: nil, bundle: nil)
viewController.view.backgroundColor = UIColor.white
let navController: UINavigationController = UINavigationController(rootViewController: viewController)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.backgroundColor = UIColor.darkGray
self.window?.rootViewController = navController
self.window!.makeKeyAndVisible()
现在在 ViewController.swift 文件中,当用户单击下面的按钮时,是我尝试添加 signInController 时的文件:
func SignIn(sender: UIButton!) {
print("Im, here")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "signInController") as! signInController
self.navigationController?.pushViewController(controller, animated: true)
}
下面是我的错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x608000269600>) doesn't contain a view controller with identifier 'signInController''
问题: 我是否必须创建 Storyboard并转到检查器 Id 以添加 Storyboard Id?但是,如果我想在不创建 Storyboard 文件的情况下完成此操作怎么办?
最佳答案
如果您不使用 Storyboard,则不应尝试从 Storyboard创建登录 View Controller 。更改代码以直接创建 View Controller :
func SignIn(sender: UIButton!) {
print("I'm, here")
let controller = signInController()
self.navigationController?.pushViewController(controller, animated: true)
}
顺便说一句 - 您需要重命名所有内容以遵循标准命名约定。类名应以大写字母开头。所有方法、变量和参数名称都应以小写字母开头。
关于ios - 尝试在不创建 Storyboard的情况下以编程方式将 Controller 文件添加到导航 Controller 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683116/