From 18837282529f46b471a48d811ea2d86564690758 Mon Sep 17 00:00:00 2001 From: Eddie Ehlin Date: Sat, 12 Jan 2013 19:36:34 +0100 Subject: AddBeanViewController now implements UITextFieldDelegate and the same goes for EditBeanViewController (new and fully implemented!). --- iBean/iBean.xcodeproj/project.pbxproj | 6 + .../UserInterfaceState.xcuserstate | Bin 32116 -> 34150 bytes iBean/iBean/AddBeanViewController.h | 2 +- iBean/iBean/AddBeanViewController.m | 20 ++- iBean/iBean/BeanCollectionBeanListViewController.m | 10 +- iBean/iBean/EditBeanViewController.h | 33 +++++ iBean/iBean/EditBeanViewController.m | 137 +++++++++++++++++++++ iBean/iBean/iPhoneStoryboard.storyboard | 59 +++++++-- 8 files changed, 248 insertions(+), 19 deletions(-) create mode 100644 iBean/iBean/EditBeanViewController.h create mode 100644 iBean/iBean/EditBeanViewController.m diff --git a/iBean/iBean.xcodeproj/project.pbxproj b/iBean/iBean.xcodeproj/project.pbxproj index c17a052..e3df510 100644 --- a/iBean/iBean.xcodeproj/project.pbxproj +++ b/iBean/iBean.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ 020C567D169F597300D8224C /* BeanCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C567C169F597300D8224C /* BeanCollection.m */; }; 020C5680169F5E9A00D8224C /* Bean+Interface.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C567F169F5E9900D8224C /* Bean+Interface.m */; }; 020C5683169F5FBB00D8224C /* BeanCollection+Interface.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C5682169F5FBB00D8224C /* BeanCollection+Interface.m */; }; + 020C568F16A1CE0E00D8224C /* EditBeanViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C568E16A1CE0E00D8224C /* EditBeanViewController.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -71,6 +72,8 @@ 020C567F169F5E9900D8224C /* Bean+Interface.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Bean+Interface.m"; sourceTree = ""; }; 020C5681169F5FBA00D8224C /* BeanCollection+Interface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BeanCollection+Interface.h"; sourceTree = ""; }; 020C5682169F5FBB00D8224C /* BeanCollection+Interface.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BeanCollection+Interface.m"; sourceTree = ""; }; + 020C568D16A1CE0E00D8224C /* EditBeanViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EditBeanViewController.h; sourceTree = ""; }; + 020C568E16A1CE0E00D8224C /* EditBeanViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EditBeanViewController.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -221,6 +224,8 @@ children = ( 020C5670169E1B0100D8224C /* AddBeanViewController.h */, 020C5671169E1B0100D8224C /* AddBeanViewController.m */, + 020C568D16A1CE0E00D8224C /* EditBeanViewController.h */, + 020C568E16A1CE0E00D8224C /* EditBeanViewController.m */, ); name = Bean; sourceTree = ""; @@ -316,6 +321,7 @@ 020C567D169F597300D8224C /* BeanCollection.m in Sources */, 020C5680169F5E9A00D8224C /* Bean+Interface.m in Sources */, 020C5683169F5FBB00D8224C /* BeanCollection+Interface.m in Sources */, + 020C568F16A1CE0E00D8224C /* EditBeanViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate b/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate index c78c824..9d36b94 100644 Binary files a/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate and b/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/iBean/iBean/AddBeanViewController.h b/iBean/iBean/AddBeanViewController.h index 155e18b..0778f36 100644 --- a/iBean/iBean/AddBeanViewController.h +++ b/iBean/iBean/AddBeanViewController.h @@ -9,7 +9,7 @@ #import @class BeanCollection; -@interface AddBeanViewController : UIViewController +@interface AddBeanViewController : UIViewController @property (nonatomic, strong) BeanCollection *beanCollection; @property (nonatomic, assign) BOOL editMode; diff --git a/iBean/iBean/AddBeanViewController.m b/iBean/iBean/AddBeanViewController.m index bebea6f..f2faa54 100644 --- a/iBean/iBean/AddBeanViewController.m +++ b/iBean/iBean/AddBeanViewController.m @@ -82,7 +82,7 @@ - (void) grinderSettingStepperChanged:(id)sender { - self.grinderSettingLabel.text = [NSString stringWithFormat:@"%1.1f", self.grinderSettingStepper.value]; + self.grinderSettingLabel.text = [NSString stringWithFormat:@"%1.0f", self.grinderSettingStepper.value]; } - (void) cancelBean:(id)sender @@ -99,7 +99,6 @@ b.name = self.beanNameTextField.text; b.grindSetting = [NSNumber numberWithDouble:self.grinderSettingStepper.value]; [self.beanCollection addBeansObject:b]; -#warning TODO - If we are in edit mode (for the bean collection) should we commit the managed context? //Go back to the bean list. [self dismissViewControllerAnimated:YES completion:nil]; @@ -109,4 +108,21 @@ #warning TODO - Messagebox telling user that error occured. } } + +/***************************************************** + Delegate + *****************************************************/ +#pragma mark - UITextField delagate related methods +//Purpose: When "return" is pressed the keyboard will go away and "change" event will be triggered. +- (BOOL)textFieldShouldReturn:(UITextField *)textField +{ + //Return key makes the keyboard go away. + if (textField == self.beanNameTextField) + { + [textField resignFirstResponder]; + return YES; + } + + return NO; +} @end diff --git a/iBean/iBean/BeanCollectionBeanListViewController.m b/iBean/iBean/BeanCollectionBeanListViewController.m index 08b1eed..5ad05d0 100644 --- a/iBean/iBean/BeanCollectionBeanListViewController.m +++ b/iBean/iBean/BeanCollectionBeanListViewController.m @@ -7,6 +7,7 @@ // #import "BeanCollectionBeanListViewController.h" +#import "EditBeanViewController.h" #import "AddBeanViewController.h" #import "BeanCollection.h" #import "Bean.h" @@ -17,8 +18,6 @@ @implementation BeanCollectionBeanListViewController -#warning TODO: Remove this method, probably not needed when using storyboards -/* - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; @@ -27,7 +26,6 @@ } return self; } -*/ - (void)viewDidLoad { @@ -60,7 +58,8 @@ } else if ([segue.identifier isEqualToString:@"EditBeanSegue"]) { - //TODO + EditBeanViewController *beanViewController = segue.destinationViewController; + [beanViewController initWithModeAndBeanCollectionAndBeanIndex:self.editMode :self.beanCollection :[self.beanListTableView indexPathForSelectedRow].row]; } } @@ -139,7 +138,6 @@ //Configure the cell and set its title Bean *currentBean = [self.beanCollection.beans objectAtIndex:indexPath.row]; -#warning TODO: This may fuck things up since the beanCollection.beans set is NOT ordered? cell.textLabel.text = currentBean.name; return cell; @@ -159,10 +157,8 @@ // Delete the row from the data source if (self.beanCollection != nil) { -#warning TODO - Do we need to remove it via delegate, and then reload the structure or just remove it? [self.beanCollection removeObjectFromBeansAtIndex: indexPath.row]; } -#warning TODO - Double check that correct bean is removed! [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { diff --git a/iBean/iBean/EditBeanViewController.h b/iBean/iBean/EditBeanViewController.h new file mode 100644 index 0000000..7d0d8fd --- /dev/null +++ b/iBean/iBean/EditBeanViewController.h @@ -0,0 +1,33 @@ +// +// EditBeanViewController.h +// iBean +// +// Created by Eddie Ehlin on 2013-01-12. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// +@class BeanCollection; + +@interface EditBeanViewController : UIViewController + +@property (nonatomic, strong) BeanCollection *beanCollection; +@property (nonatomic, assign) BOOL editMode; +@property (nonatomic, assign) NSUInteger editBeanIndex; + +/* Utility methods */ +- (void) initWithModeAndBeanCollectionAndBeanIndex:(BOOL)editMode: (BeanCollection*) bc: (NSUInteger) beanIndex; +- (void) initViewController; + +/* UI Outlets */ +@property (weak, nonatomic) IBOutlet UITextField *beanNameTextField; +@property (weak, nonatomic) IBOutlet UILabel *amountLabel; +@property (weak, nonatomic) IBOutlet UIStepper *amountStepper; +@property (weak, nonatomic) IBOutlet UILabel *grinderSettingLabel; +@property (weak, nonatomic) IBOutlet UIStepper *grinderSettingStepper; + + +/* UI Actions */ +- (IBAction) amountStepperChanged:(id)sender; +- (IBAction) grinderSettingStepperChanged:(id)sender; +- (IBAction) commitBean:(id)sender; + +@end diff --git a/iBean/iBean/EditBeanViewController.m b/iBean/iBean/EditBeanViewController.m new file mode 100644 index 0000000..5f1a84c --- /dev/null +++ b/iBean/iBean/EditBeanViewController.m @@ -0,0 +1,137 @@ +// +// EditBeanViewController.m +// iBean +// +// Created by Eddie Ehlin on 2013-01-12. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "BeanCollection+Interface.h" +#import "Bean+Interface.h" +#import "EditBeanViewController.h" + +@interface EditBeanViewController () + +@end + +@implementation EditBeanViewController + +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil +{ + self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; + if (self) { + // Custom initialization + } + return self; +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + // Do any additional setup after loading the view. +} + +- (void) viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; + [self initViewController]; +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +/***************************************************** + Utility methods + *****************************************************/ +- (void) initWithModeAndBeanCollectionAndBeanIndex:(BOOL)editMode: (BeanCollection*) bc: (NSUInteger) beanIndex +{ + self.editMode = editMode; + self.beanCollection = bc; + self.editBeanIndex = beanIndex; + if (bc == nil) + { + NSLog(@"EditBeanViewController - initWithModeAndBeanCollectionAndBeanIndex: Received bogus bean collection (nil)!"); + } +} + +- (void) initViewController +{ + NSLog(@"EditBeanViewController - initViewController"); + + if (self.editMode == YES) + { + //TODO: Update UI to refelect edit mode. + + //+ set UI controls with values from beanCollection. + NSLog(@"EditBeanViewController - in edit state"); + } + else + { + NSLog(@"EditBeanViewController - in add new state"); + } + + //Fetch bean data from the collection and populate the ui. + Bean *b = [self.beanCollection.beans objectAtIndex:self.editBeanIndex]; + if (b != nil) + { + self.beanNameTextField.text = b.name; + self.amountLabel.text = [NSString stringWithFormat:@"%1.1f", [b.amount doubleValue]]; + self.amountStepper.value = [b.amount doubleValue]; + self.grinderSettingLabel.text = [NSString stringWithFormat:@"%1.0f", [b.grindSetting doubleValue]]; + self.grinderSettingStepper.value = [b.grindSetting doubleValue]; + } +} + +/***************************************************** + UI Actions + *****************************************************/ +#pragma mark - IBActions +- (void) amountStepperChanged:(id)sender +{ + self.amountLabel.text = [NSString stringWithFormat:@"%1.1f", self.amountStepper.value]; +} + +- (void) grinderSettingStepperChanged:(id)sender +{ + self.grinderSettingLabel.text = [NSString stringWithFormat:@"%1.0f", self.grinderSettingStepper.value]; +} + +- (void) commitBean:(id)sender +{ + NSLog(@"EditBeanViewController - commitBean"); + if (self.beanCollection != nil) + { + Bean *b = [self.beanCollection.beans objectAtIndex:self.editBeanIndex]; + if (b != nil) + { + b.name = self.beanNameTextField.text; + b.amount = [NSNumber numberWithDouble:self.amountStepper.value]; + b.grindSetting = [NSNumber numberWithDouble:self.grinderSettingStepper.value]; + + //Lets return to the list + [[self navigationController] popViewControllerAnimated:YES]; + } + } +} + +/***************************************************** + Delegate + *****************************************************/ +#pragma mark - UITextField delagate related methods +//Purpose: When "return" is pressed the keyboard will go away and "change" event will be triggered. +- (BOOL)textFieldShouldReturn:(UITextField *)textField +{ + //Return key makes the keyboard go away. + if (textField == self.beanNameTextField) + { + [textField resignFirstResponder]; + return YES; + } + + return NO; +} + +@end diff --git a/iBean/iBean/iPhoneStoryboard.storyboard b/iBean/iBean/iPhoneStoryboard.storyboard index 9fbf609..1c18432 100644 --- a/iBean/iBean/iPhoneStoryboard.storyboard +++ b/iBean/iBean/iPhoneStoryboard.storyboard @@ -338,10 +338,10 @@ - + - + @@ -356,7 +356,10 @@ - + + + + - + + + + + - - - + + + + + + + + + + + + @@ -479,7 +504,10 @@ - + + + +