Archive for category QuickTip

QuickTip: Navigation Bar Image Fix in iOS 5

When working on a project, it was brought to my attention that the app’s custom navigation bar image wasn’t displaying. After looking at the code, the previous developer was using the drawRect: method to set the image. This no longer works in iOS 5 because the method never gets called, unless you subclass UINavigationBar. Below is a snippet of code to get the image to display on iOS 5 devices.

*UPDATE: Use run-time checking!*

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 5.0) {
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"portrait_image.png"] forBarMetrics: UIBarMetricsDefault];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"landscape_image.png"] forBarMetrics:UIBarMetricsLandscapePhone];
    [[UIBarButtonItem appearance] setTintColor:[UIColor colorWithRed:201.0/255 green:31.0/255 blue:56.0/255 alpha:1]];
}

QuickTip: NSUserDefaults

Just about every app that I work on utilizes NSUserDefaults in one way or another. It is quick to implement and very easy to use.

First you need to create an NSUserDefaults variable:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

Next you will need to save something that can be accessed at another time. This is very easy, we will use set methods to set values for keys. The keys can be named whatever you like, I named mine based on what object type I’m saving for this demo. Here we will save quite a few different object types:

// NSString
[userDefaults setObject:@"Your text here" forKey:@"StringKey"];
 
// Float
[userDefaults setFloat:5.22 forKey:@"FloatKey"];
 
// Double
[userDefaults setDouble:6.1234 forKey:@"DoubleKey"];
 
// NSInteger
[userDefaults setInteger:7 forKey:@"IntegerKey"];

After saving objects to your NSUserDefaults, you will want to run the the synchronize method.

[userDefaults synchronize];

Next up we will retrieve values for the keys we saved above. Here we will get the object type for the keys we specified above and assign it to a variable.

// NSString
NSString *myString = [userDefaults stringForKey:@"keyToLookupString"];
 
// Float
float myFloat = [userDefaults floatForKey:@"floatKey"];
 
// Double
double myDouble = [userDefaults doubleForKey:@"DoubleKey"];
 
// NSInteger
NSInteger myInteger = [userDefaults integerForKey:@"integerKey"];

And that is it! Hope you enjoyed this Quick Tip.

Quick Tip: Fix clear button for Calculator

I upgraded to Mac OS X Lion last month and there has been quite a few changes to get used to. One thing I noticed was that my clear key on the full keyboard doesn’t work anymore in the Calculator widget. After a little searching, I found a solution!

1. Open up a finder window and navigate to the following folder:

/Library/Widgets

2. Next locate Calculator.wdgt and ctrl-click on it, then select “Show Package Contents.”
3. Copy Calculator.js to some other location such as your desktop.
4. Open the file up in any text editor.
5. Goto line 127, and change “case 63289″ to “case 61705″. It should look like this when you are done.

case 61705:
case 27:
	key = "c";
	break;

6. Close and save the file.
7. Copy the file back to the contents of Calculator.wdgt.
8. Fire up dashboard and remove your current calculator widget and add a new one.

You’re clear button will now work in Calculator!

Here is the link to the original solution.

Quick Tip: Show Library Folder in Lion

I use my user library folder all the time, and I’m a little bit annoyed that the Library folder is now hidden in Mac OS X Lion. Here is how I changed that!

Open up Terminal and type the following and tap return:

chflags nohidden /users/<insert your username here>/library

Note: Make sure you substitute your username in that command.

Quick Tip: NSDictionary Contents

I have been doing some work with JSON recently and it can be very difficult to wade through the results. This snippet of code can very easily show you keys and values for objects within a NSDictionary.

NSString *key;
for(key in jsonDictionary){
     NSLog(@"Key: %@, Value %@", key, [jsonDictionary objectForKey: key]);
}

I hope this helps out!

Quick Tip: Changing a Sprite Image in Cocos2D

Have you ever wanted to change or swap out an image assigned to a sprite?  Well, it’s very easy to do!

We will start out by adding a Sprite Batch Node to the a layer, then we will add a new sprite.

// Add Sprite Batch Node
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"Sprites.pvr.ccz"];
[self addChild:batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Sprites.plist"];
 
// Add new sprite
CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:@"SpriteName0.png"];
sprite1.position = ccp(240,160);
[self addChild:sprite1];

All we need to do to change the image used by the sprite is to set it’s display frame like so:

[sprite1 setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"SpriteName1.png"]];

And that’s a wrap! I hope you enjoyed this quick tip, and I will be adding one a week. If you have any suggestions, please add them in the comments section.