iOS Archives

These posts are syndicated from my iOS Dev Diary over at pinkstone.co.uk.

The format is slightly different with more code snippets and less explanations. I had originally designed it as a notebook in iOS Development – now you can follow along if you’re interested.

How to save a UIImage

// let’s save our image as a JPG NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1); // get our home directory and add the save path and file name NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:@”Documents/myImage.jpg”]; // then write the file to disk [imageData writeToFile:imagePath atomically:YES];

How to combine NSString objects

Say you had two NSString objects, firstString and secondString and you’d like to combine them into a new string called comboString. You can use the convenience method stringWithFormat for this: NSString *comboString = [[NSString alloc]initWithFormat:@”First %@ and second %@”, firstString, secondString]; Any variable is passed at the end of the string, replacing each %@ symbol.

How to create a PhoneGap project for Xcode 4.5

Once you’ve downloaded the latest version from http://phonegap.com, unZIP it and copy the resulting folder to a safe directory (i.e. don’t leave it in your Downloads folder… duh!) You’ll create a fully fledged Xcode project from the command line. Make sure you cd into the directory where the create script resides. Contrary to the “Getting […]

iOS 6 Table View crashes when deployed to iOS 5

Apple have changed the UITableViewController template in iOS 6 a bit. Specifically, when you create a new UITableViewController class, it’s created using something like this (in the cellForRowAtIndexPath method): – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @”Cell”; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell… return cell; } When […]

How to create an Action Sheet and respond to it

This will bring up the UIActionSheet on iPhone devices from the bottom of the screen: // trigger a UIActionSheet UIActionSheet *myActionSheet; myActionSheet = [[UIActionSheet alloc]initWithTitle:@”My Title” delegate:self cancelButtonTitle:@”Cancel” destructiveButtonTitle:@”Cancel in Red” otherButtonTitles:@”Open in Safari”, nil]; myActionSheet.actionSheetStyle = UIActionSheetStyleAutomatic; [myActionSheet showInView:self.view]; You can add more buttons if you like by comma separating them. If you don’t […]

How to change a cell’s selection colour

By default, when you touch a cell in a UITableView, it lights up bright blue. You can use this property to change the cell’s selection style in the cellForRowAtIndexPath method: cell.selectionStyle = UITableViewCellSelectionStyleGray; Possible values are UITableViewCellSelectionStyleGray UITableViewCellSelectionStyleBlue (the default) UITableViewCellSelectionStyleNone Alternatively you can select the cell in Interface Builder and pick these values from […]