I have one storyboard in which all of my viewControllers are placed. I'm using StoryboardID
as:
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:
-(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.
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
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; 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.
You can't have a storyboard call a custom initializer.
You want to override 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:
- (instancetype)initWithCoder:(NSCoder *)aDecoder; {
[super initWithCoder: aDecoder];
//your init code goes here.
}
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:)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.