问题描述
使用storyBoard呈现ViewController:如果newViewController位于StoryBoard中,则可以使用以下方法进行显示.
Presenting ViewController Using storyBoard:if newViewController is in StoryBoard.we can present it using the following method.
let storyboard = UIStoryboard(name: "AViewController"), bundle: UIBundle.main)
let newViewcontroller = storyboard.instantiateInitialViewController() as? AViewController
self.present(newViewcontroller, animated: false, completion: nil)
是否可以提供不在StoryBoard中但其父viewcontroller具有Storyboard的ViewController
我已经以编程方式创建了一个B viewcontroller(没有StoryBoard),现在我想展示BViewController,它应该使用AViewController StoryBoard吗?
I have create a B viewcontroller programatically (No StoryBoard) ,now I would like to present BViewController and it should use the AViewController StoryBoard?
class AViewController : UIViewController
{
//this class is in storyboard which is generic design that I want to use it in different ways.
}
class BViewController : AViewController
{
....//
}
e.g.
self.present(BViewController(), animated: false, completion: nil)?
当我展示BViewcontroller时,它会为超类中的参数抛出nil.
when I present BViewcontroller it throws nil for parameters which are from super class.
推荐答案
我找到了重用故事板文件或继承故事板文件的答案.
I founded the answer to reuse the story board file or inherite the story board file.
object_setClass (设置对象的类.)将使用BViewController类覆盖AViewController的实例.因此,在AViewController之上,您可以添加更多方法.
object_setClass(Sets the class of an object.) will override instance of AViewController with BViewController Class. So on top of AViewController you can add some more methods.
当您具有类似的viewcontroller并进行很小的更改时.您必须创建不同的ViewController.使用此方法,您可以在带有故事板的基本viewcontroller上创建并重复使用该viewcontroller.
when you have similar viewcontroller with small changes. you have to create different viewcontrollers. using this method you can create on basic viewcontroller with storyboard and reuse that viewcontroller .
class BViewController{
static func vcInstanceFromStoryboard() ->BViewController? {
let storyboard = UIStoryboard(name: "AViewController"), bundle: UIBundle.main)
let instance = storyboard.instantiateInitialViewController() as? AViewController
object_setClass(instance, BViewController.self) //
return (instance as? BViewController)!
}
.....
}
This is an example of how do we use it:
let vc = BViewController.vcInstanceFromStoryboard()
self.present(vc , animation : true)
这篇关于在子级ViewController中重用父级ViewController故事板文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!