Friday, December 31, 2010

Case-insensitive NSSortDescriptor usage

I found this little gem today which was quite useful! If you are using a NSSortDescriptor to sort data using strings, you can make it case insensitive with the following:

sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

Thursday, November 4, 2010

null strings in JSONRepresentation

NSDictionary supports the JSONRepresentation method which is quite useful for generating JSON to send to servers. I recently had an issue where if one of my object's members was null, the JSONRepresentation would fail. If I set the member NSString to @"", then "" would get sent across, but what I really wanted was "member":null to be sent.

The solution is to set your object to [NSNull null], and then the correct JSONRepresentation will be created.

Tuesday, August 24, 2010

Resize a UIImage

Here's a really easy one. Resize a given UIImage that you've loaded in the following manner:

CGSize newSize = CGSizeMake(228.0, 228.0);
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Thursday, July 29, 2010

Using Next and Done with UITextFields and UIKeyboard

So you've probably seen in interface builder that its quite simple to pick which enter key you will use in your keyboard for a given UITextField. The minor complexity is that these different labels don't mean anything unless you add some code in your controller to handle the actions. Let's say you have three UITextFields:
  1. Username
  2. Password
  3. Email
You would want to set the first two to 'next' and the last to 'done' in interface builder. Then, you will need to implement UITextFieldDelegate in your View Controller, and use the following:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == txtUsername) {
[txtPassword becomeFirstResponder];
}
else if (textField == txtPassword) {
[txtEmail becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}

Monday, July 26, 2010

XCode 4 Developer Preview

Well, as registered developers we aren't really allowed to talk about it, but I'll just say WOW it is seriously nice. This is the first huge upgrade to XCode in a long time. It actually doesn't feel like Project Builder any more, and we can use git!

Friday, July 23, 2010

Accessing your UITabBarController from another UIViewController

I recently came across a situation where I wanted to show the user a UIAlertView on a view listing data if there was no data entered yet, and then direct them to an 'Add' tab in my application after they clicked OK. I ended up doing this by accessing my application's delegate which then had access to the Tab Bar Controller of the entire application.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//only one alertview so we'll just push here
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate tabBarController] setSelectedIndex:1];
}

The indexing is zero-based, as expected, so don't forget that :)

Setting the color of a UINavigationBar programmatically

If you are using a UINavigationController for your views, you can easily change the UINavigationBar's color using the following:

self.navigationController.navigationBar.tintColor = [UIColor brownColor];

and choose whatever color you wish.

Wednesday, July 21, 2010

Converting NSString to int, float, double, and bool

Here's another really basic & simple NSString tip for today to help you convert strings to various basic types. The NSString class has a bunch of conversions already built in:

float stringFloat = [myString floatValue];
int stringInt = [myString intValue]
double stringDouble = [myString doubleValue]
BOOL boolValue = [myString boolValue];

Easy NSString substring

Sadly, creating a simple substring in Objective-C isn't quite as obvious as in other languages. Here's a basic method to grab a substring of a given string before a defined character, in my case the ":" character:

NSRange end = [_contentsOfElement rangeOfString:@":"];
[myVar setName:[_contentsOfElement substringWithRange:NSMakeRange(0, end.location)]];

If you wanted grab a string between two characters, you could do:

NSRange start = [_contentsOfElement rangeOfString:@"|"];
NSRange end = [_contentsOfElement rangeOfString:@":"];
[myVar setName:[_contentsOfElement substringWithRange:NSMakeRange(start.location, end.location)]];

Monday, July 19, 2010

UITextView automatic keyboard display

I've come across a few instances where my view is simply a UITextView for the user to enter some text into, such as for a comment field, or a twitter entry, etc. In these scenarios it is best to automatically show the UIKeyboard for the user to just start typing. This is a really easy one, all you have to do is have the synchronized UITextView become the first responder to make the keyboard appear:

- (void)viewDidLoad {
    [super viewDidLoad];
    [commentTxt becomeFirstResponder];
}

You can use the same technique with UITextFields too.