The normal way to open another screen from within a FirstVC screen, so one can close it again is like this:
SecondVC *secondVC = [[SecondVC alloc] initWithNibName:@"SecondVC" bundle:nil];
secondVC.delegate = self; //needed to dismiss
[self presentModalViewController: secondVC animated: YES];
while the SecondVC.m
has to import a protocol that declares the method called to close the SecondVC
So I always have to create a protocol file SecondVCProtocol.h
which basically looks like this:
@protocol SecondVCProtocol <NSObject>
-(void)secondVCDidFinish;
@end
Then in SecondVC.m
I need to import this SecondVCProtocol.h
file and now can finally call
[self.delegate secondVCDidFinish]
I have just completed another Android app and beeing back in the iOS world, I find this rather cumbersome. - needing to define such a protocol in a separate file & needing to use a delegate - all just to do the most normal task like closing a screen...
Isn't there an easier less complex way or is this just the way it has to be done?
for example like [self dismiss]
in SecondVC - no delegate, no protocol - wouldn't his be really nice?
Many thanks!
You can just call
dismissViewControllerAnimated:completion:
on the presented viewcontroller, although it is not exactly best practice.
From Apple's documentation:
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, it automatically forwards the message to the
presenting view controller.
Also from Apple's documentation though (http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html)
When it comes time to dismiss a presented view controller, the
preferred approach is to let the presenting view controller dismiss
it. In other words, whenever possible, the same view controller that
presented the view controller should also take responsibility for
dismissing it. Although there are several techniques for notifying the
presenting view controller that its presented view controller should
be dismissed, the preferred technique is delegation.
What you describe is not the easiest pattern. Actually you should do something very similar to what you suggested would be nice. When SecondVC is ready to be dismissed it just calls, for example:
[self dismissViewControllerAnimated:YES completion:NULL];
From the UIViewController documentation:
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.