如何从故事板中获取UIViewControl对象的nib名称

时间:2021-10-09 23:08:56

I am trying to define a variable in a property within a UIViewControl class. The variable is a reference for another UIViewControl class called ViewControl.

我试图在UIViewControl类的属性中定义一个变量。该变量是另一个名为ViewControl的UIViewControl类的引用。

var handle = ViewControl(nibName: "insert_viewcontroller_id_here", bundle: nil")

How do I get the nibName? Also what is an nib name and why does it need to be referenced when the view control already has a UIViewControl class name?

我如何获得nibName?什么是nib名称,为什么在视图控件已经有UIViewControl类名时需要引用它?

Best, Alex.

1 个解决方案

#1


2  

If you want to create a new UIViewController you have two ways of doing this.

如果你想创建一个新的UIViewController,你有两种方法可以做到这一点。

1st way (programmatically way) : Create a new class that is subclass of UIViewController. In this way, you instantiate a view controller using follow code:

第一种方式(以编程方式):创建一个新类,它是UIViewController的子类。通过这种方式,您可以使用以下代码实例化视图控制器:

var viewController = ViewControl()

2nd way (xib/storyboard way) : Create new viewController using xib or storyboard. So if you choice this way and you have a view controller created in xib or storyboard you should create a new reference of view controller using follow code:

第二种方式(xib / storyboard方式):使用xib或storyboard创建新的viewController。因此,如果您选择这种方式并且在xib或storyboard中创建了视图控制器,则应使用以下代码创建视图控制器的新引用:

//Xib
var viewController = UIViewController(nibName: "ViewController", bundle: nil)

//Storyboard
var viewControllerStoryboardId = "ViewController"
var storyboardName = "Main"
var storyboard = UIStoryboard(name: storyboardName, bundle: NSBundle.mainBundle())
let viewController = storyboard.instantiateViewControllerWithIdentifier(viewControllerStoryboardId) as UIViewController!

#1


2  

If you want to create a new UIViewController you have two ways of doing this.

如果你想创建一个新的UIViewController,你有两种方法可以做到这一点。

1st way (programmatically way) : Create a new class that is subclass of UIViewController. In this way, you instantiate a view controller using follow code:

第一种方式(以编程方式):创建一个新类,它是UIViewController的子类。通过这种方式,您可以使用以下代码实例化视图控制器:

var viewController = ViewControl()

2nd way (xib/storyboard way) : Create new viewController using xib or storyboard. So if you choice this way and you have a view controller created in xib or storyboard you should create a new reference of view controller using follow code:

第二种方式(xib / storyboard方式):使用xib或storyboard创建新的viewController。因此,如果您选择这种方式并且在xib或storyboard中创建了视图控制器,则应使用以下代码创建视图控制器的新引用:

//Xib
var viewController = UIViewController(nibName: "ViewController", bundle: nil)

//Storyboard
var viewControllerStoryboardId = "ViewController"
var storyboardName = "Main"
var storyboard = UIStoryboard(name: storyboardName, bundle: NSBundle.mainBundle())
let viewController = storyboard.instantiateViewControllerWithIdentifier(viewControllerStoryboardId) as UIViewController!