Layout Managers for iOS Views
March 15th, 2013
The purpose of this blog post is to demonstrate a different approach from the Springs and Struts or Auto Layout models for view layout in an iOS application.
[banner id=“salesfolio” url=“/sales_folio.pdf”]
Sometimes in iOS, something that seems like it should be easy to do can, be a bit more complicated than you would expect or can be difficult to remember since you don’t do it very often. This is often the case for me when I have a view controller that presents another view controller modally and then during the delegate callback for the modal, I want to not only dismiss the modal itself, but the current view controller that presented it to begin with. This post will simply describe how I normally accomplish this task.
First, I present the modal view controller to do whatever task I need it to do. In this example, I am doing a signature capture:
SignatureViewController *sigViewController = [[SignatureViewController alloc] init];
sigViewController.delegate = self;
sigViewController.modalPresentationStyle = UIModalPresentationFullScreen;
sigViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:sigViewController animated:YES completion:NULL];
Then after I collect the signature in the SignatureViewController, I call the appropriate delegate in the original view controller:
if (delegate && [delegate respondsToSelector:@selector(signatureController:signatureAsBase64:savePressed:)]) {
NSString *signature = [signaturePad getSignatureAsBase64];
[delegate signatureController:self signatureAsBase64:signature savePressed:sender];
}
All pretty standard fare. Now things get just a little different than normal in the delegate. For this example we will go back to the second view controller in our navigation controller, the first being a login screen that we don’t want the user to repeat.
// It's \*VERY\* important not to animate this. Otherwise it causes problems with
// popping back to the lookup screen. Causes the views to try to
// lay themselves out while being dismissed
__block OrderPickDetailViewController *me = self;
[self dismissViewControllerAnimated:NO completion:^{
UIViewController *popTo = nil;
NSArray *viewControllers = [me.navigationController viewControllers];
if (viewControllers && [viewControllers count] > 1) {
popTo = [viewControllers objectAtIndex:1];
}
if (popTo) {
[me.navigationController popToViewController:popTo animated:YES];
} else {
[me.navigationController popToRootViewControllerAnimated:YES];
}
}];
The important things to note here are:
That’s all there is to it. Generally the biggest thing to remember in these types of situations is not to have two animations going at the same time and to sequence things logically if you can.
The purpose of this blog post is to demonstrate a different approach from the Springs and Struts or Auto Layout models for view layout in an iOS application.
iOS projects that utilize shared libraries for common components can be difficult to instrument with Google Analytics. This is one approach to make it easier.
Geb and Spock can be used to automate testing of mobile application on mobile devices, including the iPhone, iPad and Android devices.
Steve is a Principal Consultant at Object Partners where he has been focusing on developing an enterprise mobile development practice, delivering applications to client that include: Oracle Retail, The Tile Shop, St. Jude Medical, SICK USA and Donaldson Corporation. He has over 20 years of experience developing solutions from embedded to large-scale Java Enterprise web applications for diverse clients including IBM, Sun, Novell, Best Buy and Thomson Reuters.