Easy way to see (and debug) UILocalNotifications

It doesn’t take a lot of code to create a UILocalNotification but when you have multiple of them and you’re doing a lot of calendar math and things start getting hairy. To demystify that experience I created a project with a UITableViewController that prints out all of the current UILocalNotifications.

We’ll create 3 local notifications 1 minute apart for the next 3 minutes with these calls:

   [self createLocalNoticationAt:[[NSDate date] dateByAddingTimeInterval:60] withTitle:@"Every Minute 1!"];
    [self createLocalNoticationAt:[[NSDate date] dateByAddingTimeInterval:120] withTitle:@"Every Minute 2!"];
    [self createLocalNoticationAt:[[NSDate date] dateByAddingTimeInterval:180] withTitle:@"Every Minute 3!"];

Continue reading

Detect the end of a UILongPressGestureRecognizer

I am coding the previous/next buttons of a music player. I need these UIButtons to have dual purpose.

1. Tap – Previous/Next song
2. Long Press – Seek

That works, but what got me was that I didn’t know when the end of the long press was so I never knew when to stop seeking. I tried to attach an action to the UIButton on the UIControlEventTouchCancel event, but that is not correct.

This is my setup code:

    gestureSeekBackward = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(seekBackward:)];
    gestureTapBackward = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(previousSong:)];
    gestureTapBackward.numberOfTapsRequired = 1;
    [backwardButton addGestureRecognizer:gestureSeekBackward];
    [backwardButton addGestureRecognizer:gestureTapBackward];

What I needed to do was check whether the state is ended for the seek gesture. Continue reading

Change $PATH in Mac OS X Mountain Lion

I’ve had a bad time trying to get Jewelry Box running on Mountain Lion. I simply didn’t know how to edit my current $PATH.

Turns out there is a list it takes from at /etc/paths

I quickly changed the order and then after a trip to thew ‘brew doctor’ everything seems fine and now I’ve got ruby 2.0.0!

After much searching this is the great resource which I found:
http://daniel.hepper.net/blog/2011/02/change-order-of-path-entries-on-mac-os-x/

Chaining UIView animations with blocks

I really love UIView animations. They are so simple and elegant. This little chain I will make will fade in a UIView (or descendant) from the left, wait a moment, then fade out to the left. In general you can animate all of these properties: frame,bounds,center,transform,alpha,backgroundColor,and contentStretch in a UIView animation block.

For this example I created a single view application and put a UIImageView in the middle of the screen. UIImageView is a direct descendant of UIView so it is perfect for this exercise.
UIImageView
Continue reading

UITableView Disclosure Indicator on a UIButton

Although it is non-standard. I needed to match pixel-perfect to a design, so I created this. On a UITableView it is called ‘Disclosure Indicator’ as opposed to the one available from UIButton which is ‘Detail Disclosure’.

table_indicator

It is 50×80 with a transparent background. Use this image on top of a button or UIImageView. Resize it to whatever size you’d like your button to be. Apple recommends a hit target of no less than 40×40. I sized it to 10×16 in my storyboard, but I am using a transparent button overlay so the size doesn’t matter.
Continue reading

Configure a UIScrollView entirely in Storyboard with Interface Builder

Usually even if you drop in a UIScrollView, put a UIView into that and align them correctly it still does not work. Why? It’s UIScrollView.contentSize. Now we can easily do this in code, but it is also easily accomplished in Interface Builder.

The first thing I’ve done is drop in a UIScrollView, pop a UIView inside of it and then populate my controls into said UIView. We can verify this by looking at the hierarchal view.
UIView inside UIScrollView
Continue reading

Searching an NSArray of NSDictionaries

NSPredicate is the simplest way to do it.

Given Data like this:

NSArray *theArray = @[@{@"id":1,"name":@"one"},@{@"id":2,"name":@"two"},@{@"id":3,"name":@"three"}];
NSPredicate *p = [NSPredicate predicateWithFormat:@"id = 291"]; //fiber
NSArray *matchedDicts = [theArray filteredArrayUsingPredicate:p];

There is much more to NSPredicate. I used exact matching, but matching with BEGINSWITH, CONTAINS, ENDSWITH, and LIKE. In some examples you’ll also see [c] or [cd] next to these keywords. The ‘c’ means it searches case insensitively and the ‘d’ means that an ‘o’ with an umlaut is still just an ‘o’.

Getting the Day of the Week from an NSDate

I am trying to get a custom day of the week format like this: Su, M, T, W, Th, F, S.

I’d really like to be able to compare numbers and get 0-6 instead of the full name(Wednesday) or the 3 letter day (Wed). I found this great resource for the NSDateFormatter codes. It turns out that ‘E’ is the 3 letter name and just ‘e’ is the number! The problem I found was that ‘e’ was not zero based, so I had to put a pad into my NSArray that gives my custom weekday string.

	NSDate *now = [NSDate date];
	NSDateFormatter *nowDateFormatter = [[NSDateFormatter alloc] init];
	NSArray *daysOfWeek = @[@"",@"Su",@"M",@"T",@"W",@"Th",@"F",@"S"];
	[nowDateFormatter setDateFormat:@"e"];
	NSInteger weekdayNumber = (NSInteger)[[nowDateFormatter stringFromDate:newDate] integerValue];
	NSLog(@"Day of Week: %@",[daysOfWeek objectAtIndex:weekdayNumber]);

Output (It’s Wednesday):
Day of Week: W

Walking through the code I created a date named ‘now’. Then I created an NSDateFormatter followed by an NSArray of the custom daysOfWeek. I’m using Objective-C literals so I don’t need to end in ‘nil’. I also added a string for the zero element as this output is not zero based so it will never be zero, but it will go from 1-7 for Sunday-Saturday. I then use the format string of @”e” to get just that number and in the next line I create an NSInteger and it’s not a pointer because NSIntegers is basically just a C int. The NSDateFormatter will return a string no matter what and since I know I’m getting a number I just use the NSString ‘integerValue’ to cast and it returns an NSUInteger. NSUInteger is an ‘unsigned int’ variable and NSInteger is just an ‘int’. I could have used either but these numbers will always be small so an NSInteger works just fine.

Asterix Options

NSString *asterixOnVar;
NSString * asterixInAir;
NSString* asterixOnTypdef;

They all declare that it is a pointer to an NSString. There is no difference except readability. I prefer the first version because it shows the type and it is clear that the variable is a pointer.

Constraints Killing You? Blame Auto-Layout.

I recently made a new project and when I ran it the UIButtons and UIViews would all change size and weird things would happen. I now understand that it was merely the auto-layout completely freaking out and trying to fit things into a space in which they already fit.

Are constraints moving around your interface builder elements too much?

Here is what the constraints look like in Xcode. They seem to show up automatically and cannot be manually deleted. I deleted a few of them before I realized that they’d come back even stronger. I’m sure it works for someone to fix their app into both the 3.5″(old + retina) and 4″(iphone 5) versions of the iPhone now. I’m sure the new versions of XCode will work quickly to fix this feature. When I was struggling to figure out what is wrong I realized constraints would prevent me from moving UIViews around the screen. That more than anything else really bothered me. To impinge on normal functionality is a cardinal sin.
Continue reading