Permalink
Browse files

More fixes to ItemDetailViewController/ItemEditViewController

  • Loading branch information...
PureAbstract committed Jun 1, 2012
1 parent c7d740b commit e4d993f5d830d1342b8b066857c099bd1f3db87a
@@ -11,7 +11,6 @@
@interface ItemDetailViewController : UITableViewController {
PWItem *item_;
- NSMutableArray *values_;
}
@property (nonatomic,retain) PWItem *item;
-(id)initWithItem:(PWItem *)item;
@@ -7,13 +7,25 @@
//
#import "ItemDetailViewController.h"
+#import "ItemEditViewController.h"
@implementation ItemDetailViewController
@synthesize item = item_;
#pragma mark -
#pragma mark Initialization
+enum {
+ kIndexTitle = 0,
+ kIndexLogin,
+ kIndexPassword,
+ kIndexUrl,
+ kIndexEmail,
+ kIndexNotes,
+
+ kIndexMax,
+};
+
static NSString *cellLabels [] = {
@"Title",
@"Login",
@@ -28,13 +40,6 @@ -(id)initWithItem:(PWItem *)item
self = [super initWithStyle:UITableViewStyleGrouped];
if( self ) {
self.item = item;
- values_ = [[NSMutableArray alloc] initWithCapacity:6];
- [values_ addObject:item.title];
- [values_ addObject:item.login];
- [values_ addObject:item.password];
- [values_ addObject:item.url];
- [values_ addObject:item.email];
- [values_ addObject:item.notes];
}
return self;
}
@@ -50,18 +55,38 @@ - (id)initWithStyle:(UITableViewStyle)style {
}
*/
+-(void)onEditSaved:(ItemEditViewController *)controller
+{
+ // Update the view
+ if( controller && controller.item ) {
+ [[NSNotificationCenter defaultCenter] postNotificationName:kPWDataUpdated
+ object:controller.item];
+ [self.tableView reloadData];
+ }
+}
+
+-(void)onEdit:(UIBarButtonItem *)sender
+{
+ ItemEditViewController *controller = [[ItemEditViewController alloc] initWithItem:self.item
+ target:self
+ action:@selector(onEditSaved:)];
+ [self presentModalViewController:controller animated:YES];
+ [controller release];
+}
+
#pragma mark -
#pragma mark View lifecycle
-/*
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
+ self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
+ target:self
+ action:@selector(onEdit:)] autorelease];
}
-*/
/*
- (void)viewWillAppear:(BOOL)animated {
@@ -97,7 +122,7 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
- return values_.count;
+ return kIndexMax;
}
@@ -120,7 +145,21 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
// Configure the cell...
NSUInteger itemIndex = indexPath.section;
cell.tag = itemIndex;
- cell.textLabel.text = (NSString *)[values_ objectAtIndex:itemIndex];
+ NSString *text = nil;
+ switch( itemIndex ) {
+ case kIndexTitle: text = item_.title; break;
+ case kIndexLogin: text = item_.login; break;
+ case kIndexPassword: text = item_.password; break;
+ case kIndexUrl: text = item_.url; break;
+ case kIndexEmail: text = item_.email; break;
+ case kIndexNotes: text = item_.notes; break;
+ default:
+ NSAssert( NO, @"Index out of range" );
+ text = @"";
+ break;
+ }
+
+ cell.textLabel.text = text;
return cell;
}
@@ -211,7 +250,6 @@ - (void)viewDidUnload {
- (void)dealloc {
- [values_ release];
[item_ release];
[super dealloc];
}
@@ -18,6 +18,9 @@
IBOutlet UITextField *emailField_;
IBOutlet UITextView *notesField_;
IBOutlet UINavigationBar *navBar_;
+
+ id<NSObject> target_;
+ SEL action_;
}
@property (nonatomic,retain) PWItem *item;
@property (nonatomic,retain) IBOutlet UITextField *titleField;
@@ -28,7 +31,8 @@
@property (nonatomic,retain) IBOutlet UITextView *notesField;
@property (nonatomic,retain) IBOutlet UINavigationBar *navBar;
+-(id)initWithItem:(PWItem *)item target:(id<NSObject>)target action:(SEL)action;
+
-(IBAction)onSave:(UIBarButtonItem *)sender;
-(IBAction)onCancel:(UIBarButtonItem *)sender;
-
@end
@@ -21,7 +21,43 @@ @implementation ItemEditViewController
@synthesize notesField = notesField_;
@synthesize navBar = navBar_;
+
#pragma mark -
+#pragma mark Item/Control data xfer
+// Put all this in one place for ease of maintainance...
+-(void)transferData:(BOOL)saveToItem {
+ if( item_ ) {
+ if( saveToItem ) {
+ item_.title = titleField_.text;
+ item_.login = loginField_.text;
+ item_.password = passwordField_.text;
+ item_.url = urlField_.text;
+ item_.email = emailField_.text;
+ item_.notes = notesField_.text;
+ } else {
+ titleField_.text = item_.title;
+ loginField_.text = item_.login;
+ passwordField_.text = item_.password;
+ urlField_.text = item_.url;
+ emailField_.text = item_.email;
+ notesField_.text = item_.notes;
+ }
+ }
+}
+#pragma mark -
+#pragma mark View lifecycle
+
+-(id)initWithItem:(PWItem *)item target:(id<NSObject>)target action:(SEL)action
+{
+ self = [super init];
+ if( self ) {
+ self.item = item;
+ target_ = target;
+ action_ = action;
+ }
+ return self;
+}
+
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
@@ -37,20 +73,12 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
- if( item_ ) {
- titleField_.text = item_.title;
- loginField_.text = item_.login;
- passwordField_.text = item_.password;
- urlField_.text = item_.url;
- emailField_.text = item_.email;
- notesField_.text = item_.notes;
-
- }
// Magic...
notesField_.layer.borderWidth = 1;
notesField_.layer.borderColor = [[UIColor grayColor] CGColor];
notesField_.layer.cornerRadius = 5;
+ [self transferData:NO];
[titleField_ becomeFirstResponder];
}
@@ -110,16 +138,35 @@ - (BOOL)textFieldShouldReturn:(UITextField *)textField
}
return YES;
}
+
+#pragma mark -
+#pragma mark UITextViewDelegate
+// TODO: Scroll up here...
+//- (void)textViewDidBeginEditing:(UITextView *)textView;
+
+
+#pragma mark -
+#pragma mark Target
+-(void)endEditing:(BOOL)save
+{
+ if( target_ && action_ ) {
+ [target_ performSelector:action_ withObject:(save ? self : nil)];
+ }
+ [self dismissModalViewControllerAnimated:YES];
+}
+
#pragma mark -
#pragma mark Bar Button handlers
-(IBAction)onSave:(UIBarButtonItem *)sender
{
- [self dismissModalViewControllerAnimated:YES];
+ [self transferData:YES];
+ [self endEditing:YES];
}
-(IBAction)onCancel:(UIBarButtonItem *)sender
{
- [self dismissModalViewControllerAnimated:YES];
+ self.item = nil;
+ [self endEditing:NO];
}
@end
View
@@ -39,12 +39,25 @@ -(NSUInteger)count
return [self.data count];
}
+-(void)sortByTitle
+{
+ [self.data sortUsingComparator:^(id obj1, id obj2 ) {
+ PWItem *p1 = (PWItem *)obj1;
+ PWItem *p2 = (PWItem *)obj2;
+ return [p1.title caseInsensitiveCompare:p2.title];
+ }];
+}
+
-(void)addObject:(PWItem *)item
{
NSAssert1( [item isKindOfClass:[PWItem class]], @"Expected a PWItem, got %@", item );
NSAssert( ![self containsObject:item], @"Object already present" );
[self.data addObject:item];
- // TODO: Resort collection - notify observers
+ // Resort... this probably isn't the best place to do it...
+ [self sortByTitle];
+ // TODO: notify anyone who cares
+ [[NSNotificationCenter defaultCenter] postNotificationName:kPWDataUpdated
+ object:self];
}
-(PWItem *)objectAtIndex:(NSUInteger)index
View
@@ -8,6 +8,8 @@
#import <Foundation/Foundation.h>
+extern NSString * const kPWDataUpdated;
+
@interface PWItem : NSObject<NSCoding> {
// Note: It may be a better plan to store these as an array,
// with specified indices.
View
@@ -9,6 +9,10 @@
#import "PWItem.h"
#import "NSCoder+withDefaults.h"
+
+NSString * const kPWDataUpdated = @"kPWDataUpdated";
+
+
#define kPropKeyTitle @"t"
#define kPropKeyLogin @"l"
#define kPropKeyPassword @"p"
@@ -237,11 +237,21 @@ -(void)onSearchButton:(NSObject *)sender
[self showSearchBar:self.tableView.tableHeaderView==nil];
}
+-(void)editViewSaved:(ItemEditViewController *)controller
+{
+ if( controller && controller.item ) {
+ // Save the new item..
+ [self.data addObject:controller.item];
+ [self.tableView reloadData];
+ }
+}
+
-(void)onAddButton:(NSObject *)sender
{
- ItemEditViewController *controller = [ItemEditViewController new];
PWItem *newItem = [PWItem new];
- controller.item = newItem;
+ ItemEditViewController *controller = [[ItemEditViewController alloc] initWithItem:newItem
+ target:self
+ action:@selector(editViewSaved:)];
[newItem release];
[self presentModalViewController:controller animated:YES];
[controller release];
Oops, something went wrong.

0 comments on commit e4d993f

Please sign in to comment.