Gold Challenge - Programmatically


I thought it would be great experience to do this challenge programmatically rather using the xib file. I saw that no one else has posted this so feel free to take a look (possibly improve upon?) for reference:


// ChangeDateViewController.h
// Homepwner
// Created by TheGamingArt on 8/14/12.
// Copyright © 2012 TheGamingArt. All rights reserved.

#import <UIKit/UIKit.h>

#import “DetailViewController.h”

@class BNRItem;

@interface ChangeDateViewController : UIViewController{
IBOutlet UIDatePicker *datePicker;
IBOutlet UIButton *changeDateButton;

//@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) BNRItem *itemWithDate;




// ChangeDateViewController.m
// Homepwner
// Created by TheGamingArt on 8/14/12.
// Copyright © 2012 TheGamingArt. All rights reserved.

#import “ChangeDateViewController.h”
#import “BNRItem.h”

@interface ChangeDateViewController ()


@implementation ChangeDateViewController
//@synthesize date;
@synthesize itemWithDate;

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    return self;

[[self navigationItem] setTitle:[itemWithDate itemName]]; // changes the Navigation bar’s title to itemName

[super viewDidLoad];
[[self view]setBackgroundColor:[UIColor groupTableViewBackgroundColor]];

datePicker = [[UIDatePicker alloc] init];

changeDateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//[changeDateButton setBackgroundColor:[UIColor whiteColor]];
[changeDateButton setTitle:@"Change Date" forState:UIControlStateNormal];
changeDateButton.frame = CGRectMake(100.0, 100.0, 120.0, 50.0); CGPointMake([super view].center.x, (datePicker.bounds.size.height) *1.3);
[changeDateButton addTarget:self action:@selector(chageDate:) forControlEvents:UIControlEventTouchUpInside]; // allows button to interact and send messages when pressed.

// = [[super view] center];

NSDate *date = [itemWithDate dateCreated];

[datePicker setDate:date];
[datePicker setDatePickerMode:UIDatePickerModeDate];

[[self view] addSubview:datePicker];
[[self view] addSubview:changeDateButton];
//[[self view] insertSubview:changeDate aboveSubview:datePicker];


  • (void)viewDidLoad

    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@“Fraud” message:@“Warning Insurance Fraud!” delegate:nil cancelButtonTitle:@“OK” otherButtonTitles:nil];

    [myAlert show];


//NSLog(@“This works”);
[[self navigationController] popViewControllerAnimated:YES];



[super viewWillDisappear];

[itemWithDate setDateCreated:[datePicker date]];


  • (void)viewDidUnload
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

  • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    return (interfaceOrientation == UIInterfaceOrientationPortrait);



I put the button from the xib file

rest of the code identical



changes in DetailViewController.m

[code]- (IBAction)changeDate:(id)sender {

ChangeDateViewController *changeDateViewController = [[ChangeDateViewController alloc] init];
[changeDateViewController setItem:self.item];

changeDateViewController.item.dateCreated = self.item.dateCreated;

[[self navigationController] pushViewController:changeDateViewController animated:YES];


note that I used dot notation just for fun.