Skip to main content

Posts

Showing posts from September, 2012

Return to root view controller from multiple modal presentations or segues (Xcode for iOS)

If you are returning to the root view controller from a series of modally presented view controllers then the following code will work in iOS6

[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
In iOS5 it would be

[self.view.window.rootViewController dismissModalViewControllerAnimated:YES];
If you want to dismiss all modally presented view controllers from the App Delegate, for example from

- (void)applicationWillResignActive:(UIApplication *)application
then you'd write


[self.window.rootViewController dismissViewControllerAnimated:NO completion:nil];

Or for iOS5

[self.window.rootViewController dismissModalViewControllerAnimated:NO];

Extending classes with categories in Xcode for iOS

A category extends the functionality of Apple's in-built classes without subclassing. We do this by first of all creating a header and implementation file for the category by using the Objective-C category template. Next we add our method to the category's .m file:

#import "UIColor+seeThroughBlue.h"

@implementation UIColor (seeThroughBlue)
+(UIColor*)seeThroughBlueColor {
    return [UIColor colorWithRed:0 green:0 blue:1 alpha:0.5];
}
@end
(Note: I'm extending UIColor and have called the category seeThroughBlue - Xcode has added UIColor+ to the filenames for convenience)

Next we need to add this method to the .h file:


#import <UIKit/UIKit.h>

@interface UIColor (seeThroughBlue)
+(UIColor*)seeThroughBlueColor;
@end

You'll notice that the name of the category (seeThroughBlue) isn't the same as the filename, because it doesn't need to be.

Finally, to make it globally available we add it to the appName-Prefix.pch file (in your supporting files folde…

TouchDraw OS X and iOS

I've always found TouchDraw (iOS) to be an extremely useful tool for the iPad. It is just so quick and simple to layout a design that can be later refined. It is also very good for flow charts. The number of good quality shapes and stock vectors is really handy and there are even some architectural ones to assist in redesigning your rooms. So all in all it is a good all round multipurpose vector drawing app that certainly gets things started even if you don't use it for the finishing touches. And for the price it is a bargain, especially when you pair the OS X and iPad versions together in your workflow.

If you already use the iPad version, then the OS X version works exactly as you'd expect making good use of the OS X input methods and additional screen space. It also adds refinements to the way files are exported (to PNG, JPEG, TIFF, GIF, BMP, PDF, EPS, SVG, or Visio).

Container View Controllers, Parent View Controllers and Child View Controllers in Xcode (iOS 5 onwards)

For this demo you'll need to 
1) create a New Project in Xcode called AddChildViewControllers

2) add two new pairs of files, which will be subclasses of UIViewController (using File->New->File, selecting Objective-C class as a file type, and entering UIViewController in the "Subclass of" box)

3) in the implementation (.m) files of the first and second child view controllers, which I've called FirstChildViewController and SecondChildViewController, you'll need to add two new methods:


-(void)willMoveToParentViewController:(UIViewController *)parent

-(void)didMoveToParentViewController:(UIViewController *)parent

4) The first method of the FirstChildViewController:


-(void)willMoveToParentViewController:(UIViewController *)parent

will be called from AddChildViewControllersViewController.m once you've instantiated the new view controller with 


FirstChildVC = [[FirstChildViewController alloc] init];
remembering first to import the class with:


#import "FirstChi…