// // AddPersonController.m // TouchEdLightly // // Created by Andrew Kinnie on 5/17/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "AddPersonController.h" @implementation AddPersonController @synthesize personArray; /* // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { // Custom initialization } return self; } */ #pragma mark - #pragma mark Save and Cancel - (IBAction) save: (id) sender { if (person != nil) { // we have an existing person we're editing // remove it from the array so the new one will be created with the changed values [personArray removeObject:person]; self.person = nil; } NSDate *now = [NSDate date]; // create new Person for the person to add to the array. Person *newPerson = [[Person alloc] init]; newPerson.firstName = self.firstNameField.text; newPerson.lastName = self.lastNameField.text; // save to remote server [newPerson saveRemote]; // add to iPhone array: [personArray addObject:newPerson]; NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(caseInsensitiveCompare:)]; NSSortDescriptor *firstNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(caseInsensitiveCompare:)]; [personArray sortUsingDescriptors:[NSArray arrayWithObjects:lastNameDescriptor, firstNameDescriptor, nil]]; [lastNameDescriptor release]; [firstNameDescriptor release]; [newPerson release]; [self dismissModalViewControllerAnimated:YES]; } - (IBAction) cancel: (id) sender { [self dismissModalViewControllerAnimated:YES]; } /* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */ - (void)viewWillAppear: (BOOL)animated { [super viewWillAppear:animated]; NSLog(@"Registering for keyboard events"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@ selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@ selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; // Initially the keyboard is hidden, so reset our variable keyboardVisible = NO; if (self.person != nil) { firstNameField.text = person.firstName; lastNameField.text = person.lastName; createdLabel.text = [person.createdAt description]; updatedLabel.text = [person.updatedAt description]; } } - (void)viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self]; } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)] autorelease]; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)] autorelease]; } //- (void)keyboardDidShow:(NSNotification *)notification { // if(keyboardVisible){ // NSLog(@"keyboard already visible. Ignore notification."); // return; // } // //NSLog(@"resizing"); // NSDictionary *info = [notification userInfo]; // NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey]; // CGSize keyboardSize = [value CGRectValue].size; // CGRect viewFrame = self.view.frame; // viewFrame.size.height -= keyboardSize.height; // scrollView.frame = viewFrame; // keyboardVisible = YES; //} //- (void)keyboardDidHide:(NSNotification *)notification { // if(keyboardVisible){ // NSLog(@"keyboard already hidden. Ignore notification."); // return; // } // //NSLog(@"resizing"); // NSDictionary *info = [notification userInfo]; // NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey]; // CGSize keyboardSize = [value CGRectValue].size; // CGRect viewFrame = self.view.frame; // viewFrame.size.height += keyboardSize.height; // scrollView.frame = viewFrame; // keyboardVisible = NO; //} /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [personArray release]; [super dealloc]; } @end