NSString Concatenation Using Categories and NSArray Literals.

August 23, 2013

A while back I had posted on concatenating NSString objects. Now that we have literal notation, let’s see how much easier and terse we can make this, hmmmm?

Here is the category on NSArray:

@interface NSArray (StringUtilities)
- (NSString *)string;
@end
@implementation NSArray (StringUtilities)
- (NSString *)string;
{
    return [self componentsJoinedByString:@""];
}
@end

Before, using my category on NSString would make concatenation work like this:

NSString *test = [NSString concatenate:@"This", @" is", @" a", @" test.", nil];

Now, the same thing with the category on NSArray looks like this:

NSString *test = @[ @"This", @" is", @" a", @" test." ].string;

Not as wordy and easier to type. I think I like it! Here’s a gist!

Advertisements

JMSimplePageViewController

August 20, 2013

Sometimes you just want a simple UIPageViewController that you can just feed a bunch of view controllers (either already newed up or a list of class names) has a dotted page control at the bottom, and just works the way you would expect it to.

Well, here you go: https://github.com/jmenter/JMSimplePageViewController


Using UIViewController Class Inheritance with Nib Files

August 17, 2013

If you’re like me, you love using .xib files to specify what your user interfaces are to look like. One unfortunate side effect of using .xib files is that they do not easily lend themselves to class inheritance. Described here is a technique for using class inheritance with your UI elements laid out in .xib files.

For this example, we have a class hierarchy that looks like this:

UIViewController (included in UIKit)

JMViewController

JMSubViewController

JMAlertnateViewController

JMViewController can be though of as the root of our specific view controller class hierarchy. It contains a view, IBOutlets, UILabels, UIButtons, etc. It also has a method that is going to be used by the view controller cubclasses:

– (void)addSubviewsFromSuperclass;

This method gets called in the – (void)viewDidLoad method of the subclasses and does this following:

  1. Gets a pointer to the current self.view property
  2. Loads the nib of the superclass, connects all inherited IBOutlets, and gets the main view (element 0)
  3. Reassigns the self.view property (the loadNibNamed method assigned it to the main view of the nib)
  4. Adds all subviews of the nib’s main view to our current view hierarchy
  5. (all this happens in a try/catch block in case something goes wrong with the nib loading process)

So, basically, the way to use class hierarchy with UIViewControllers that use .xib files is to call a special method that will unpack the view hierarchy of the superclass and add all elements to the current instance.

Check out the example project here!