Gold Challenge - Programmatically


#1

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

[code]//
// 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;

-(IBAction)chageDate:(id)sender;

@end[/code]

ChangeDateViewController.m

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

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

@interface ChangeDateViewController ()

@end

@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;
    }

-(void)viewWillAppear:(BOOL)animated{
[[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);
changeDateButton.center= 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.

// datePicker.center = [[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];

}

-(IBAction)chageDate:(id)sender{
//NSLog(@“This works”);
[[self navigationController] popViewControllerAnimated:YES];

}

-(void)viewWillDisappear:(BOOL)animated{

[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);
    }

@end
[/code]


#2

I put the button from the xib file

rest of the code identical

works


#3

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];

}[/code]

note that I used dot notation just for fun.