简体   繁体   English

从情节提要中添加了自定义初始化子视图

[英]Custom initialise subview added from storyboard

I have a subclass of UIViewController that I want to add from the storyboard. 我有一个要从情节UIViewController中添加的UIViewController的子类。

So I'm using what seems the standard methodology: 所以我使用的似乎是标准方法:

SubViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SubViewControllerID"];
[self addChildViewController:svc];
[self.view addSubview:svc.view];

Which is fine but what if I want to call a custom init method on the subview? 哪个很好,但是如果我想在子视图上调用自定义init方法呢?

I can do something like: 我可以做类似的事情:

svc = [svc initWithFoo:@"Hello"];

Which seems to have to go after the addSubview call inorder for it to work. 似乎必须在addSubview调用之后才能运行。

Is this the best way to do this? 这是最好的方法吗?

Seems a bit unorthodox. 似乎有点非常规。 Calling an init method on an object that has already been created seems like its no longer truly an init method. 在已经创建的对象上调用init方法似乎不再是真正的init方法。

Maybe I should call it setWithFoo: or something and not have it return anything? 也许我应该将其命名为setWithFoo:或其他什么东西不让它返回任何东西?

If you want to do additional setup to your view controller after you instantiate it form the storyboard you can create some methods in the view controller's class and call them after the instantiate method fo the storyboard. 如果要在从情节提要实例化视图控制器后对其进行其他设置,可以在视图控制器的类中创建一些方法,并在情节提要的instantiate方法之后调用它们。

But be careful, if you try to make changes on any UI component in those methods, they wont be applied, and probably the app will crash. 但是请注意,如果您尝试在这些方法中的任何UI组件上进行更改,则将不会应用它们,并且应用程序可能会崩溃。 So use those methods to set params to the View Controller like array of objects, or any kind of data, and apply the UI changes for the view controller's view in viewDidLoad/viewWillAppear/viewDidAppear methods of your view controller. 因此,请使用这些方法为视图控制器设置参数,例如对象数组或任何类型的数据,然后在视图控制器的viewDidLoad/viewWillAppear/viewDidAppear方法中为视图控制器的视图应用UI更改。

SubViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SubViewControllerID"];

will cause the SubViewController to be inited with it's - (id)initWithCoder:(NSCoder *)decoder {} method. 会导致SubViewController的- (id)initWithCoder:(NSCoder *)decoder {}方法被- (id)initWithCoder:(NSCoder *)decoder {}

Override that method (don't forget to call super ) 重写该方法(别忘了调用super

Essentially I think the answer is that you can't use custom initialisers on ViewControllers added from the storyboard. 本质上,我认为答案是您不能在从情节提要添加的ViewController上使用自定义初始化程序。 Instead you have to set properties directly or through a method at the appropriate time in the life cycle as stated above. 相反,您必须如上所述在生命周期的适当时间直接或通过方法设置属性。

Also as mentioned, the VC will be instantiated through initWithCoder , so calling an additional initialiser might be superfluous(?). 同样如前所述,VC将通过initWithCoder实例化,因此调用额外的初始化程序可能是多余的(?)。

I encountered problems trying to use a custom initialiser that contains a call to super if I called it before the subview was added. 我在尝试使用自定义初始化程序时遇到了问题,如果我在添加子视图之前调用了它,则该初始化程序包含对super的调用。 I would just get a blank view added, I think because the superclass doesn't seem to know about the storyboard at that point. 我想只是添加一个空白视图,我想是因为超类当时似乎不了解情节提要。 I had more success removing the call to super but that seems wrong. 我在取消对super的调用方面取得了更大的成功,但这似乎是错误的。

This case would be more pertinent when adding subviews to a scrollview. 将子视图添加到滚动视图时,这种情况将更有意义。 For simplicity I left this out of my example. 为简单起见,我将其排除在示例之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM