我正在尝试将视图控制器推送到导航控制器而不设计故事板。这是可能的,因为我是Swift的新手吗?我在appdelegate.swift文件中创建了我的导航控制器:
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)
}
以下是 错误:
由于未捕获的异常终止应用' NSInvalidArgumentException',原因:'故事板(< UIStoryboard:0x608000269600>)并不包含带有标识符' signInController&#的视图控制器39;'
问题: 我是否必须创建故事板并转到检查员ID以添加故事板ID?但是,如果我想在不创建故事板文件的情况下完成此任务呢?
答案 0 :(得分:1)
你想在不使用故事板的情况下创建一个viewController,而你仍然试图在你的代码中使用故事板。
尝试这样的事情:
var window: UIWindow?
var navigationController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
// Override point for customization after application launch.
self.window!.backgroundColor = UIColor.white
self.window!.makeKeyAndVisible()
let myViewController: YourNewViewController = YourNewViewController()
self.navigationController = UINavigationController(rootViewController: myViewController!)
self.window!.rootViewController = self.navigationController
return true
}
答案 1 :(得分:1)
如果您不使用故事板,则不应尝试从故事板创建登录视图控制器。更改代码以直接创建视图控制器:
func SignIn(sender: UIButton!) {
print("I'm, here")
let controller = signInController()
self.navigationController?.pushViewController(controller, animated: true)
}
BTW - 您需要重命名所有内容以遵循标准命名约定。类名应以大写字母开头。所有方法,变量和参数名称都应以小写字母开头。