The typical approach taken when one needs to pass/share variables among view controllers (using storyboards) is to grab a reference to the target viewcontroller
in prepareForSegue:
method and use property injection like so:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"Segue_Name"])
{
// Get reference to the target view controller (optional casting)
DestinationViewController *vc = (DestinationViewController *)segue.destinationViewController;
// Pass any objects/properties
vc.whateverObject = object;
}
}
The problem with this approach (as I see it in the context of the domain I'm dealing with on a current project) is that the dependency on the object being passed/injected is not explicit.
Though this kind of freedom (being able to inject dependencies after initialization) is welcome in some use cases where flexibility is required, most of the time I prefer the feeling of safety constructor injection provides.
Especially in a language like objective-c where passing around and sending messages to nil is allowed/common, hunting bugs where I forgot to set a property at the right point is tiresome even with unit tests to back me up.
What I'd like to know is if there is a way to use a custom initializer, one I could provide with as many arguments/dependencies I need.
Is there a way I can manually control the initialization of segue.destinationViewController
?
As long as I know you can not. If you want your custom initialization you have to do it without segues - using init
, store your DestinationViewController
manually and do whatever you need to do with it pushViewController
or presentViewController
.
Another drawback of using segue is that DestinationViewController
will be recreated every time.
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.