gpt4 book ai didi

ios - 如何在 Storyboard 中使用 ViewController 的自定义初始化

转载 作者:可可西里 更新时间:2023-11-01 06:21:25 28 4
gpt4 key购买 nike

我有一个 Storyboard,其中放置了我所有的 viewController。我将 StoryboardID 用作:

AddNewPatientViewController * viewController =[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"addNewPatientVC"];
[self presentViewController:viewController animated:YES completion:nil];

AddNewPatientViewController 中,我添加了一个自定义的初始化方法或构造函数,您可以这样说:

-(id) initWithoutAppointment
{
self = [super init];
if (self) {
self.roomBedNumberField.hidden = true;
}
return self;
}

所以我的问题是通过使用上面的 wat 呈现 View Controller ,我如何使用我制作的自定义 init init 它。

我已经尝试将此作为上述代码的替换,但没有成功。

AddNewPatientViewController *viewController = [[AddNewPatientViewController alloc] initWithoutAppointment];
[self presentViewController:viewController animated:YES completion:nil];

最佳答案

使用这种方法不是最好的主意。首先,我想建议你在实例化之后设置这个属性;会更好

如果你无论如何都想创建这样的构造函数,你可以将带有实例化的代码放入其中,所以它看起来像

-(id) initWithoutAppointment
{
self = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"addNewPatientVC"];
if (self) {
self.roomBedNumberField.hidden = true;
}
return self;
}

但这不会是一个好的代码

已编辑

可能是样式问题,但我宁愿不这样做,因为 View Controller 不必了解 UIStoryboard;如果你想有这样的方法,最好把它移到一些单独的工厂。如果我选择在没有 Storyboard 的其他项目中使用此 VC,或者使用 Storyboard,但使用另一个名称,则很容易出错。

关于ios - 如何在 Storyboard 中使用 ViewController 的自定义初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40635329/

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