|
| 1 | + |
| 2 | +/* |
| 3 | + File: EditingViewController.m |
| 4 | + Abstract: A generic table view controller responsible for editing a field of data (text or date). |
| 5 | + The controller defines a protocol to communicate changes to the view controller that manages the object being edited. |
| 6 | + |
| 7 | + Version: 1.0 |
| 8 | + |
| 9 | + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple |
| 10 | + Inc. ("Apple") in consideration of your agreement to the following |
| 11 | + terms, and your use, installation, modification or redistribution of |
| 12 | + this Apple software constitutes acceptance of these terms. If you do |
| 13 | + not agree with these terms, please do not use, install, modify or |
| 14 | + redistribute this Apple software. |
| 15 | + |
| 16 | + In consideration of your agreement to abide by the following terms, and |
| 17 | + subject to these terms, Apple grants you a personal, non-exclusive |
| 18 | + license, under Apple's copyrights in this original Apple software (the |
| 19 | + "Apple Software"), to use, reproduce, modify and redistribute the Apple |
| 20 | + Software, with or without modifications, in source and/or binary forms; |
| 21 | + provided that if you redistribute the Apple Software in its entirety and |
| 22 | + without modifications, you must retain this notice and the following |
| 23 | + text and disclaimers in all such redistributions of the Apple Software. |
| 24 | + Neither the name, trademarks, service marks or logos of Apple Inc. may |
| 25 | + be used to endorse or promote products derived from the Apple Software |
| 26 | + without specific prior written permission from Apple. Except as |
| 27 | + expressly stated in this notice, no other rights or licenses, express or |
| 28 | + implied, are granted by Apple herein, including but not limited to any |
| 29 | + patent rights that may be infringed by your derivative works or by other |
| 30 | + works in which the Apple Software may be incorporated. |
| 31 | + |
| 32 | + The Apple Software is provided by Apple on an "AS IS" basis. APPLE |
| 33 | + MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION |
| 34 | + THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS |
| 35 | + FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND |
| 36 | + OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
| 37 | + |
| 38 | + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL |
| 39 | + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 40 | + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 41 | + INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, |
| 42 | + MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED |
| 43 | + AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), |
| 44 | + STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE |
| 45 | + POSSIBILITY OF SUCH DAMAGE. |
| 46 | + |
| 47 | + Copyright (C) 2010 Apple Inc. All Rights Reserved. |
| 48 | + |
| 49 | + */ |
| 50 | + |
| 51 | +#import "EditingViewController.h" |
| 52 | +#import "RootViewController.h" |
| 53 | + |
| 54 | +@implementation EditingViewController |
| 55 | + |
| 56 | +@synthesize textField, editedObject, editedPropertyKey, editedPropertyDisplayName, editingDate, datePicker, sourceController; |
| 57 | + |
| 58 | + |
| 59 | +#pragma mark - |
| 60 | +#pragma mark View lifecycle |
| 61 | + |
| 62 | +- (void)viewDidLoad { |
| 63 | + // Set the title to the user-visible name of the field. |
| 64 | + self.title = editedPropertyDisplayName; |
| 65 | + |
| 66 | + // Configure the save and cancel buttons. |
| 67 | + UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)]; |
| 68 | + self.navigationItem.rightBarButtonItem = saveButton; |
| 69 | + [saveButton release]; |
| 70 | + |
| 71 | + UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)]; |
| 72 | + self.navigationItem.leftBarButtonItem = cancelButton; |
| 73 | + [cancelButton release]; |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +- (void)viewWillAppear:(BOOL)animated { |
| 78 | + |
| 79 | + [super viewWillAppear:animated]; |
| 80 | + |
| 81 | + // Update user interface according to state. |
| 82 | + if (editingDate) { |
| 83 | + textField.hidden = YES; |
| 84 | + datePicker.hidden = NO; |
| 85 | + NSDate *date = [editedObject valueForKey:editedPropertyKey]; |
| 86 | + if (date == nil) date = [NSDate date]; |
| 87 | + datePicker.date = date; |
| 88 | + } |
| 89 | + else { |
| 90 | + textField.hidden = NO; |
| 91 | + datePicker.hidden = YES; |
| 92 | + textField.text = [editedObject valueForKey:editedPropertyKey]; |
| 93 | + textField.placeholder = self.title; |
| 94 | + [textField becomeFirstResponder]; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +#pragma mark - |
| 100 | +#pragma mark Save and cancel operations |
| 101 | + |
| 102 | +- (IBAction)save { |
| 103 | + |
| 104 | + // Pass the current value to the source controller, then pop. |
| 105 | + if (editingDate) { |
| 106 | + [sourceController setValue:datePicker.date forEditedProperty:editedPropertyKey]; |
| 107 | + } |
| 108 | + else { |
| 109 | + [sourceController setValue:textField.text forEditedProperty:editedPropertyKey]; |
| 110 | + } |
| 111 | + |
| 112 | + [self.navigationController popViewControllerAnimated:YES]; |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +- (IBAction)cancel { |
| 117 | + // Don't pass the current value to the edited object, just pop. |
| 118 | + [self.navigationController popViewControllerAnimated:YES]; |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +#pragma mark - |
| 123 | +#pragma mark Memory management |
| 124 | + |
| 125 | +- (void)dealloc { |
| 126 | + [datePicker release]; |
| 127 | + [editedObject release]; |
| 128 | + [editedPropertyKey release]; |
| 129 | + [editedPropertyDisplayName release]; |
| 130 | + [super dealloc]; |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +@end |
| 135 | + |
0 commit comments