· ipad

iPad: Redrawing the screen

As I mentioned in a post I wrote last week I’ve been writing a little iPad application to parse a cctray feed and then display the status of the various builds on the screen.

The way I’ve been doing this is by dynamically adding labels to the view and colouring the background of those labels red or green depending on the build status.

FirstViewController.h

@interface FirstViewController : UIViewController {
	...
	NSMutableArray *dynamicallyAddedFields;
}

FirstViewController.m

@implementation FirstViewController

- (void)viewDidLoad {
	...
	// parse xml and put projects into 'theDelegate.projects

	NSInteger x = 30;
	NSInteger y = 0;
	NSInteger column1 = true;

	dynamicallyAddedFields = [[NSMutableArray alloc] init];

	for(Project *project in theDelegate.projects) {
		if(column1) {
			x = 30;
			y = y + 110;
			column1 = false;
		} else {
			x = 290;
			column1 = true;
		}

		UILabel *aLabel	= [self createLabelFrom:project withXCoordinate:x withYCoordinate:y];

		[dynamicallyAddedFields addObject:aLabel];
		[self.view addSubview:aLabel];
	}
}
-(UILabel*) createLabelFrom:(Project *)project withXCoordinate:(NSInteger)x withYCoordinate:(NSInteger)y  {
	// code to create a label
}

The full code is on github and this is what the application looks like when launched in the simulator:

ipad.jpg

I’m storing each of the labels inside 'dynamicallyAddedFields' so that I can easily remove them and then redraw new ones with the new feed url when the user clicks on the 'Go' button. I saw a similar idea suggested in an iPhone Dev SDK article.

The 'Touch Up Inside' event of the 'Go' button is hooked up to the 'changedFeedUrl' method inside my view controller as described in Brandon Treb’s blog.

The code to handle the button being pressed is as follows:

- (IBAction) changedFeedUrl:(id) sender {
	for (id item in dynamicallyAddedFields) {
		[item removeFromSuperview];
	}
	[dynamicallyAddedFields removeAllObjects];

	// parse xml and put projects into 'theDelegate.projects

	NSInteger x = 30;
	NSInteger y = 0;
	NSInteger column1 = true;
	for(Project *project in theDelegate.projects) {
		if(column1) {
			x = 30;
			y = y + 110;
			column1 = false;
		} else {
			x = 290;
			column1 = true;
		}

		UILabel *aLabel	= [self createLabelFrom:project withXCoordinate:x withYCoordinate:y];
		[dynamicallyAddedFields addObject:aLabel];
		[self.view addSubview: aLabel];

	}
}

In order to redraw all the labels I had to first remove them all from their super view (line 3).

We then have the same code as in the 'loadView' method - copy and pasted for now - to add the new labels to the screen.

Although this code does what I want it is very hacky so I’d be grateful for any tips on how I can achieve the same thing in a cleaner way.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket