gpt4 book ai didi

ios - 如何调用initWithCoder?

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:09:22 25 4
gpt4 key购买 nike

我有 UIViewControllerUIView 子类(比如说 ViewSubClass)。我正在使用 Storyboard。在 mainStoryBoard.storyBord 中,我放置了一个 UIView 并设置了自定义类 ViewSubClass。现在我不想在 UIViewController 中创建 ViewSubClass 的实例,例如 ViewSubClass *subClassInstance = [[ViewSubClass alloc] init]。无需在 ViewController 中创建任何 ViewSubClass 实例,我想调用 initWithCoder 方法。当我运行我的应用程序时,应该调用 ViewSubClassinitWithCoder 方法。我怎样才能做到这一点?

最佳答案

在您的 View 类 .m 文件中添加以下代码

-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
}
return self;
}

并在 View 类中添加文件名,如下图所示。

enter image description here

关于ios - 如何调用initWithCoder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19157157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com