Bronze Challenge - The lazy way


#1

Hi everyone,

Here is my solution to the Bronze Challenge.

As you can see, it’s a very easy/lazy way to complete the challenge.

Everything happens in the DetailViewController.

First of all, add a new instance variable to DetailViewController.h for the new UIPopoverController:

Then , in DetailViewController.m, I implemented the “- (IBAction)showAssetTypePicker:(id)sender” method :

[code]- (IBAction)showAssetTypePicker:(id)sender
{
[[self view] endEditing:YES];

if ([assetTypePickerPopover isPopoverVisible]) {
    [assetTypePickerPopover dismissPopoverAnimated:YES];
    assetTypePickerPopover = nil;
    return;
}

AssetTypePicker *assetTypePicker = [[AssetTypePicker alloc] init];
[assetTypePicker setItem:item];

[[assetTypePicker tableView] setDelegate:self];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    CGRect rect = [[self view] convertRect:[sender bounds] fromView:sender];
    assetTypePickerPopover = [[UIPopoverController alloc] initWithContentViewController:assetTypePicker];
    [assetTypePickerPopover setDelegate:self];
    [assetTypePickerPopover setPopoverContentSize:CGSizeMake(300, 300)];
    [assetTypePickerPopover presentPopoverFromRect:rect
                                        inView:[self view]
                      permittedArrowDirections:UIPopoverArrowDirectionAny
                                                animated:YES];
}

}[/code]

Note that in this method I tell the AssetTypePicker’s tableView that the DetailViewController will be its delegate (to easily dismiss the UIPopoverController and update the interface (assetTypeButton)

Then I implemented (in DetailViewController.m) the UITableViewDelegate Method “- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath” like this:

[code]- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

[cell setAccessoryType:UITableViewCellAccessoryCheckmark];

NSArray *allAssets = [[BNRItemStore sharedStore] allAssetTypes];
NSManagedObject *assetType = [allAssets objectAtIndex:[indexPath row]];

[item setAssetType:assetType];

// Update DetailViewController Interface
NSString *typeLabel = [[item assetType] valueForKey:@"label"];

if (!typeLabel) {
    typeLabel = @"None";
}

[assetTypeButton setTitle:[NSString stringWithFormat:@"Type: %@", typeLabel] forState:UIControlStateNormal];

// Dismiss UIPopOverController
[assetTypePickerPopover dismissPopoverAnimated:YES];
assetTypePickerPopover = nil;

}[/code]

I’m sure this is not the cleanest way to complete the challenge but it’s the easiest I found, I just reused pieces of code we saw earlier in the book and set de the tableView delegate to be the DetailViewController instead of AssetTypePicker.

Hope this helps !