Bronze, Silver, Gold Challenges


#1

Hello dear folks, i bring my solutions for the 3 challenges, hope i can help anyone!

DetailViewController.m:

[code]//
// JDMDetailViewController.m
// Homepwner
//
// Created by Jorge Decuir Martinez on 27/04/14.
// Copyright © 2014 Jorge Decuir Martinez. All rights reserved.
//

#import “JDMDetailViewController.h”
#import “JDMChangeDateViewController.h”
#import “JDMItem.h”

@interface JDMDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *serialField;
@property (weak, nonatomic) IBOutlet UITextField *valueField;
@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@end

@implementation JDMDetailViewController

#pragma mark - View Methods

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear];

JDMItem *item = self.item;

self.nameField.text = item.itemName;
self.serialField.text = item.serialNumber;
self.valueField.text = [NSString stringWithFormat:@"%d",item.valueInDollars];

//You need an NSDateFormatter that will turn a date into a simple string
static NSDateFormatter *dateFormatter;
if (!dateFormatter) {
    dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateStyle = NSDateFormatterMediumStyle;
    dateFormatter.timeStyle = NSDateFormatterNoStyle;
}

//Use filtered NSDate object to set dateLabel
self.dateLabel.text = [dateFormatter stringFromDate:item.dateCreated];

}

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear];

//clear first responder
[self.view endEditing:YES];

//"Save" changes to item
JDMItem *item = self.item;
item.itemName = self.nameField.text;
item.serialNumber = self.serialField.text;
item.valueInDollars = [self.valueField.text intValue];

}

//Silver Challenge Solution

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.valueField resignFirstResponder];
[self.nameField resignFirstResponder];
[self.serialField resignFirstResponder];
}

#pragma mark - Customized Accessors Methods

-(void)setItem:(JDMItem *)item
{
_item = item;
self.navigationItem.title = _item.itemName;
}

//Gold Challenge Solution

  • (IBAction)changeItemDate:(id)sender
    {
    JDMChangeDateViewController *changeDateVC = [[JDMChangeDateViewController alloc] init];
    changeDateVC.item = self.item;
    [self.navigationController pushViewController:changeDateVC animated:YES];
    }
    @end[/code]

ChangeItemDateViewController.m:

[code]//Gold challenge Solution

#import “JDMChangeDateViewController.h”
#import “JDMItem.h”

@interface JDMChangeDateViewController ()
@property (weak, nonatomic) IBOutlet UIDatePicker *changeItemDate;
@end

@implementation JDMChangeDateViewController

#pragma mark - View Methods

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    [self.changeItemDate setDatePickerMode:UIDatePickerModeDate];

}

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear];
[self.changeItemDate setDate:self.item.dateCreated animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear];
[self changeItemDate:self.item];
}

#pragma mark - Helper Methods

-(void)changeItemDate:(JDMItem *)item
{
item.dateCreated = [self.changeItemDate date];
}

@end
[/code]

I liked this challenge because many of the things i’ve done i did it truly myself without searching a lot and using Apple Docs, so i’m a little proud. Hope i can help!