简体   繁体   English

如何在Storyboard中使用ViewController的自定义init

[英]How to use custom init of ViewController in Storyboard

I have one storyboard in which all of my viewControllers are placed. 我有一个故事板,其中放置了所有的viewControllers。 I'm using StoryboardID as: 我正在使用StoryboardID

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

In AddNewPatientViewController I've added a custom init method or constructor you can say as: AddNewPatientViewController我添加了一个自定义的init方法或构造函数,你可以这样说:

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

So my question is by using the above wat of presenting view controller how can I init it with this custom init I've made. 所以我的问题是通过使用上面提到的视图控制器的wat,我如何使用我已经制作的这个自定义initinit它。

I've tried doing this as areplacement of above code but it didn't work. 我已尝试将此作为上述代码的替换,但它不起作用。

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

It's not the best idea to use this approach. 使用这种方法不是最好的主意。 At first, I want to suggest you to set this property after instantiation; 首先,我建议你在实例化后设置这个属性; it would be better 会更好

If you anyway want to make such constructor, you can place your code with instantiation to it, so it will look like 如果你想要制作这样的构造函数,你可以将代码放在实例化中,所以看起来就像

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

but it won't be a good code 但它不是一个好的代码

EDITED EDITED

May be it's the question of style, but I would rather prefer not to do this because view controller doesn't have to know about UIStoryboard; 可能是风格问题,但我宁愿不这样做,因为视图控制器不必知道UIStoryboard; if you want to have such method, it would be better to move it to some separate factory. 如果你想拥有这样的方法,最好把它移到一个单独的工厂。 If I chose to use this VC in other projects without Storyboard, or with storyboards, but with another name, it will be error-prone. 如果我选择在没有Storyboard或故事板的其他项目中使用此VC,但使用其他名称,则会容易出错。

You can't have a storyboard call a custom initializer. 您不能让故事板调用自定义初始化程序。

You want to override init(coder:) . 你想要覆盖init(coder:) That's the initializer that gets called when a view controller is created from a storyboard (or from a nib, for that matter.) 这是从故事板(或从笔尖创建视图控制器)时调用的初始化程序。

Your code might look something like this: 您的代码可能如下所示:

Objective-C: Objective-C的:

- (instancetype)initWithCoder:(NSCoder *)aDecoder; {
    [super initWithCoder: aDecoder];
    //your init code goes here.
}

Swift: 迅速:

required init?(coder: NSCoder)  {
  //Your custom initialization code goes here.
  print("In \(#function)")
  aStringProperty = "A value"
  super.init(coder: coder)
}

Note that in Swift your initializer must assign values to all non-optional properties before you call super.init, and that you must call super.init() (or in this case, super.init(coder:) . 请注意,在Swift中,初始化程序必须在调用super.init之前为所有非可选属性赋值,并且必须调用super.init() (或者在本例中为super.init(coder:)

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

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