Skip to content

Commit 2d61302

Browse files
author
Darcy Liu
committed
add SimpleUndo
1 parent db9b338 commit 2d61302

26 files changed

Lines changed: 2241 additions & 1 deletion

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,4 +1412,12 @@ The application illustrates the following techniques: configuring and responding
14121412

14131413
[URL](https://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007419)
14141414

1415-
Last Revision: Version 1.11, 2010-06-23
1415+
Last Revision: Version 1.11, 2010-06-23
1416+
1417+
#SimpleUndo#
1418+
1419+
The root view controller displays information (title, author, and copyright date) about a book. The user can edit this information by tapping Edit in the navigation bar. When editing starts, the root view controller creates an undo manager to record changes. The undo manager supports up to three levels of undo and redo. When the user taps Done, changes are considered to be committed and the undo manager is disposed of.
1420+
1421+
[URL](https://developer.apple.com/library/ios/#samplecode/SimpleUndo/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008408)
1422+
1423+
Last Revision: Version 1.1, 2010-06-23

SimpleUndo.zip

43.2 KB
Binary file not shown.

SimpleUndo/Classes/Book.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
/*
3+
File: Book.h
4+
Abstract: Simple class to represent a book.
5+
Version: 1.0
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
46+
47+
*/
48+
49+
@interface Book : NSObject {
50+
NSString *title;
51+
NSString *author;
52+
NSDate *copyright;
53+
}
54+
55+
@property (nonatomic, retain) NSString * title;
56+
@property (nonatomic, retain) NSString * author;
57+
@property (nonatomic, retain) NSDate * copyright;
58+
59+
@end

SimpleUndo/Classes/Book.m

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
/*
3+
File: Book.m
4+
Abstract: Simple class to represent a book.
5+
Version: 1.0
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2010 Apple Inc. All Rights Reserved.
46+
47+
*/
48+
49+
#import "Book.h"
50+
51+
@implementation Book
52+
53+
@synthesize title, author, copyright;
54+
55+
- (void)dealloc {
56+
[title release];
57+
[author release];
58+
[copyright release];
59+
[super dealloc];
60+
}
61+
62+
@end
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
/*
3+
File: EditingViewController.h
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+
52+
/*
53+
Protocol to define communication between editing view controller and the view controller that configured it.
54+
*/
55+
@protocol PropertyEditing
56+
- (void)setValue:(id)newValue forEditedProperty:(NSString *)field;
57+
@end
58+
59+
60+
@interface EditingViewController : UIViewController {
61+
62+
UITextField *textField;
63+
UIDatePicker *datePicker;
64+
65+
id editedObject;
66+
NSString *editedPropertyKey;
67+
NSString *editedPropertyDisplayName;
68+
69+
BOOL editingDate;
70+
71+
id <PropertyEditing> sourceController;
72+
}
73+
74+
@property (nonatomic, retain) IBOutlet UITextField *textField;
75+
@property (nonatomic, retain) IBOutlet UIDatePicker *datePicker;
76+
77+
/*
78+
Aspects of the object being edited:
79+
The object itself, the name of the property being edited, and the name to display to the user.
80+
*/
81+
@property (nonatomic, retain) id editedObject;
82+
@property (nonatomic, retain) NSString *editedPropertyKey;
83+
@property (nonatomic, retain) NSString *editedPropertyDisplayName;
84+
85+
@property (nonatomic, assign, getter=isEditingDate) BOOL editingDate;
86+
87+
@property (nonatomic, retain) id <PropertyEditing> sourceController;
88+
89+
- (IBAction)cancel;
90+
- (IBAction)save;
91+
92+
@end
93+
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Comments
 (0)