Ios 从xib文件加载TestViewController(一切正常,但我有一个问题)
我有一个Ios 从xib文件加载TestViewController(一切正常,但我有一个问题),ios,swift,uiviewcontroller,uinavigationcontroller,xib,Ios,Swift,Uiviewcontroller,Uinavigationcontroller,Xib,我有一个TestViewController.swift和一个TestViewController.xib展示TestViewController的布局 let vc = TestViewController(nibName: nil, bundle: nil) self.navigationController?.pushViewController(vc, animated: true) let storyboard = UIStoryboard(name: "storybard
TestViewController.swift
和一个TestViewController.xib
展示TestViewController的布局
let vc = TestViewController(nibName: nil, bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
let storyboard = UIStoryboard(name: "storybardName", bundle: nil)
let customViewController = storyboard.instantiateViewController(withIdentifier: "CustomViewController")
要加载此视图控制器,我有:
一切正常,但如果我调用空初始值设定项:
let vc = TestViewController()
self.navigationController?.pushViewController(vc, animated: true)
或者没有名字
let vc = TestViewController(nibName: nil, bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
该应用程序也可以继续工作而不做任何更改。明确指定xib文件的名称有什么意义?在这种情况下,三个调用之间有什么区别?嗯
在本部分中,如果您仅使用代码创建视图,您将使用,因为您从未告诉Xcode加载nib文件,因此nib文件从未加载,因此您在nib文件中创建的项从未加载
let vc = TestViewController()
self.navigationController?.pushViewController(vc, animated: true)
在本部分中,您告诉vc需要首先从nib文件加载,您必须使用包含视图控制器的nib文件的名称
let vc = TestViewController(nibName: nil, bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
let storyboard = UIStoryboard(name: "storybardName", bundle: nil)
let customViewController = storyboard.instantiateViewController(withIdentifier: "CustomViewController")
所以,
如果您使用的是nib文件,您将使用第二个,但如果您只使用代码创建视图控制器,请使用第一个,但我通常使用故事板而不是使用nib文件创建视图控制器
let vc = TestViewController(nibName: nil, bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
let storyboard = UIStoryboard(name: "storybardName", bundle: nil)
let customViewController = storyboard.instantiateViewController(withIdentifier: "CustomViewController")
那么
在本部分中,如果您仅使用代码创建视图,您将使用,因为您从未告诉Xcode加载nib文件,因此nib文件从未加载,因此您在nib文件中创建的项从未加载
let vc = TestViewController()
self.navigationController?.pushViewController(vc, animated: true)
在本部分中,您告诉vc需要首先从nib文件加载,您必须使用包含视图控制器的nib文件的名称
let vc = TestViewController(nibName: nil, bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
let storyboard = UIStoryboard(name: "storybardName", bundle: nil)
let customViewController = storyboard.instantiateViewController(withIdentifier: "CustomViewController")
所以,
如果您使用的是nib文件,您将使用第二个,但如果您只使用代码创建视图控制器,请使用第一个,但我通常使用故事板而不是使用nib文件创建视图控制器
let vc = TestViewController(nibName: nil, bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
let storyboard = UIStoryboard(name: "storybardName", bundle: nil)
let customViewController = storyboard.instantiateViewController(withIdentifier: "CustomViewController")
“在本部分中,如果您仅使用代码创建视图,您将使用,因为您从未告诉Xcode加载nib文件,因此nib文件从未加载,因此您在nib文件中创建的项目从未加载。”为什么?我使用代码创建视图,xib中的视图加载得非常好“在本部分中,如果您仅使用代码创建视图,您将使用,因为您从未告诉Xcode加载nib文件,所以nib文件从未加载,所以您在nib文件中创建的项从未加载。”为什么?我使用代码创建视图,xib中的视图加载得非常好