Custom Code Snippets in xCode 4.2.1

This is a quick and dirty xCode tip for creating your own custom code snippets to reuse in your projects.

How to convert an xCode Project to ARC (Automatic Reference Counting) in xCode 4

Converting an xCode project to ARC:

If you develop your apps in xCode using ARC (Automatic Reference Counting), and like most of us, you read tutorials, and download sample projects to learn, no doubt you’ve come accross some sample code and/or sample projects that don’t use ARC. In order to convert an xCode project to ARC, you simply go the the Edit menu, and select “Edit > Refactor > Convert to Objective-C ARC.” Follow the onscreen instructions and your set. :)

How to Create and Add a Subview Programmatically in xCode 4

In this tutorial we will be generating a subview programatically and loading it into a view using xCode 4.2.1 (with ARC).

Step 1

Create a new “Single View” Project, and in the ViewController.h file, make the following changes:

1
2
3
4
5
6
7
8
9
10
11
12
#import <uikit /UIKit.h>

@interface ViewController : UIViewController {
   
    UIView *subView;
   
}

@property (nonatomic, retain) UIView *subView;

@end
</uikit>

Read More

How to generate a random float between 0 and 1 in Objective-C (xCode)

I have a pretty useful code snippet to share today. This code will produce a random float (a number with a decimal point) between 0 and 1. It will also produce 0.0 and 1.0. This can come in handy when setting the alpha property in xCode to a random opacity, which requires a float value in the range of 0.0 – 1.0.

1
float x = arc4random() % 11 * 0.1;

In the code snippet above, “float x” declares our variable as a float and names it x.

The “arc4Random() % 11″ method generates a random number (which is a whole number, or integer) between (and including) 0 and 10 because if you count to ten starting with zero instead of one, you have 11 numbers, not 10.

That number gets multiplied by “* 0.1″, which is the equivalent of dividing it by 10, thus producing our decimals.

This gives us a minimum value of 0, i.e. 0 * 0.1 = 0, and a maximum value of 1, i.e. 10 * 0.1 = 1.

To see it in action, we can use NSLog to print our results:

1
2
float x = arc4random() % 11 * 0.1;
NSLog(@"x = %f", x);

The output would look like this in xCode:

1
2012-01-26 10:19:42.383 animation[4999:f803] x = 0.500000

If you ran it repeatedly, your output would look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2012-01-26 10:51:32.249 animation[5050:f803] x = 0.400000
2012-01-26 10:51:32.999 animation[5050:f803] x = 0.800000
2012-01-26 10:51:34.367 animation[5050:f803] x = 1.000000
2012-01-26 10:51:35.531 animation[5050:f803] x = 0.900000
2012-01-26 10:51:36.101 animation[5050:f803] x = 0.200000
2012-01-26 10:51:36.711 animation[5050:f803] x = 0.800000
2012-01-26 10:51:37.593 animation[5050:f803] x = 0.500000
2012-01-26 10:51:38.977 animation[5050:f803] x = 0.400000
2012-01-26 10:51:40.633 animation[5050:f803] x = 0.800000
2012-01-26 10:51:41.280 animation[5050:f803] x = 0.100000
2012-01-26 10:51:41.812 animation[5050:f803] x = 0.600000
2012-01-26 10:51:42.514 animation[5050:f803] x = 0.000000
2012-01-26 10:51:57.578 animation[5050:f803] x = 0.500000
2012-01-26 10:51:58.070 animation[5050:f803] x = 0.000000
2012-01-26 10:51:58.624 animation[5050:f803] x = 0.000000
2012-01-26 10:51:59.102 animation[5050:f803] x = 0.100000

If you want to print the results with no trailing zeros, change the %f in your NSLog to %g.

1
2
float x = arc4random() % 11 * 0.1;
NSLog(@"x = %g", x);

And the output would look like this:

1
2
3
4
5
6
7
8
9
10
2012-01-26 11:04:52.741 animation[5137:f803] x = 0
2012-01-26 11:04:53.497 animation[5137:f803] x = 0.7
2012-01-26 11:04:54.083 animation[5137:f803] x = 0.5
2012-01-26 11:04:54.615 animation[5137:f803] x = 0
2012-01-26 11:04:55.083 animation[5137:f803] x = 0
2012-01-26 11:04:55.615 animation[5137:f803] x = 0.8
2012-01-26 11:04:56.177 animation[5137:f803] x = 0.2
2012-01-26 11:04:57.295 animation[5137:f803] x = 1
2012-01-26 11:04:57.881 animation[5137:f803] x = 0.1
2012-01-26 11:04:58.741 animation[5137:f803] x = 0.3

If you want to specify how many decimal places you want to print, change the %g in your NSLog to %.2f, where 2 is the number of decimal places you want.

1
2
float x = arc4random() % 11 * 0.1;
NSLog(@"x = %.2f", x);

And the output would look like this:

1
2
3
4
5
6
7
8
9
2012-01-26 11:08:02.058 animation[5180:f803] x = 0.80
2012-01-26 11:08:02.901 animation[5180:f803] x = 0.40
2012-01-26 11:08:03.431 animation[5180:f803] x = 0.10
2012-01-26 11:08:04.033 animation[5180:f803] x = 0.60
2012-01-26 11:08:04.659 animation[5180:f803] x = 0.00
2012-01-26 11:08:12.847 animation[5180:f803] x = 0.30
2012-01-26 11:08:13.331 animation[5180:f803] x = 0.00
2012-01-26 11:08:13.823 animation[5180:f803] x = 1.00
2012-01-26 11:08:14.261 animation[5180:f803] x = 0.70

Customize the appearance of UITableView and UITableViewCell in xCode 4

Welcome to 46 Media. This is our inaugural blog post, as well as our first xCode tutorial! We will be posting not only video tutorials here, but little code snippets here and there. (All of our tutorials will be using xCode 4 or later) Subscribe via email if you would like to get notified of new posts :)

In this tutorial we will be customizing a UITableView in xCode 4. We will go over how to generate a UIImaghe and set it as a background for a table, as well as changing the background of the individual UITabbleViewCells. Lastly, we’ll set a custom color for the UItableView separatorColor to achieve a custom color for the border that separates each row and surrounds the table view itself.

If you don’t feel like watching the entire video, or you’re a more advanced user, here’s the quick overview in a few simple steps.

This tutorial uses xCode 4.2.1.

Step 1:

Create a new xCode project and choose a Master-Detail iOS Application. In xCode 4, the Master-Detail Application template already has a Table View and corresponding controller included. Otherwise, customize your existing project with a few simple lines of code in this tutorial. Name it whatever you want.

Step 2:

In whichever viewController you’ll be using the UITableView, add a UIView and @property as follows to the .h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//  tableMasterViewController.h

#import <UIKit/UIKit.h>

@class tableDetailViewController;

@interface tableMasterViewController : UITableViewController {
   
    UIView *bgView;
   
}

@property (nonatomic, retain) UIView *bgView;

@property (strong, nonatomic) tableDetailViewController *detailViewController;

@end

Step 3:

In the corresponding .m file, synthesize your UIView.

1
@synthesize bgView;

Step 4:

In the same .m file, initialize the view (bgView) inside the viewDidLoad function using CGRectMake. Then set the backgrondColor to a colorWithPatternImage, and use an image you add to your project. Set the image appropriately using [UIimage imageNamed:@""]; to define the background of our view as an actual image, even though we are using backgroundColor.

1
2
3
4
5
6
7
8
9
10
11
- (void)viewDidLoad
{
    // Initialize our UIView and give it a rectangle to put stuff in.
    bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
   
    // Set a background image for our view.
    bgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
   
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

Step 5:

Again the same .m file, we need to set our table view background, separator style, and cell configurations in the cellForRowAtIndexPath method. I listed the entire method here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
   
    // Set the background of the table to a UIView named bgView.
    tableView.backgroundView = bgView;
   
    // Set a custom color for the separator lines.
    tableView.separatorColor = [UIColor colorWithHue:0 saturation:0 brightness:33 alpha:0.2];
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    // Configure the cell.
    cell.textLabel.text = NSLocalizedString(@"Detail", @"Detail");
   
    // Color for the text in each cell.
    cell.textLabel.textColor = [UIColor whiteColor];
   
    // Set the background of each cell to an image. (320px x 44px).
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell"]];
   
    // Set the background color of the 'textLabel portion of each cell to transparent.
    cell.textLabel.backgroundColor = [UIColor clearColor];
   
    return cell;
}

And that’s it. with just a few lines of code, we have a custom tale view with endless possibilities.

Download Source – (Table.zip) 258K