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 convert an integer into a string (text object)

I keep forgetting how to do this so here it is: if you have an integer and you want to print it out, say in a UILabel you can use the initWithFormat method of NSString like so: NSString *myNumber; myNumber = [[NSString alloc] initWithFormat:@”%d”, myInteger]; self.myLabel.text = myNumber; The secret here is the @”%d” which […]

How to share data between Navigation Controllers

The Root Navigation Controller can serve as a data model. Each View Controller connected to the Navigation Controller via push segue can access its properties like so: ((MyNavController *)self.parentViewController).mySourceProperty Here’s an example. MyNavController is the class for the Navigation Controller. This snipped is called from any View Controller in sequence and assumes we have an […]

How to trigger a Modal Segue

Once you’ve connected two View Controllers via a Segue in the storyboard you need to give it an identifier so we can call it in code like so: [self performSegueWithIdentifier:@”showDetail” sender:self]; Dismiss it via an action like this: [self dismissViewControllerAnimated:YES completion:nil];

How to play an Audio Alert

Just like playing a standard sound file, you can also choose to play an alert. The difference is that this will make the iPhone vibrate if it’s set to silent. The code is nearly identical to the standard sound file with the exception of the very last line of code: SystemSoundID soundID; NSString *soundFile = […]

How to play a sound effect

This is a rather cryptic setup, but here’s how it works: SystemSoundID soundID; NSString *soundFile = [[NSBundle mainBundle]pathForResource:@”yourfile” ofType:@”wav”]; AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID); AudioServicesPlaySystemSound(soundID); This snippet assumes you’re using a file called yourfile.wav which needs to present in your project. You’ll also need to import the AudioToolbox Framework and import it in your header file.

How to create an Alert View

Here’s how we can create a simple pop-up window via the UIAlertView instance. It can have a title, some info text and several buttons (OK, Cancel, etc). We’ll only deal with one button and not worry about how to read out which button value has been pressed. And here’s how we build this on Objective-C: […]