IOS设计模式之四(备忘录模式,命令模式)
- - (void)saveCurrentState
- {
- // When the user leaves the app and then comes back again, he wants it to be in the exact same state
- // he left it. In order to do this we need to save the currently displayed album.
- // Since it‘s only one piece of information we can use NSUserDefaults.
- [[NSUserDefaultsstandardUserDefaults] setInteger:currentAlbumIndex forKey:@"currentAlbumIndex"];
- }
- - (void)loadPreviousState
- {
- currentAlbumIndex = [[NSUserDefaultsstandardUserDefaults] integerForKey:@"currentAlbumIndex"];
- [self showDataForAlbumAtIndex:currentAlbumIndex];
- }
- [self loadPreviousState];
- [[NSNotificationCenterdefaultCenter] addObserver:self selector:@selector(saveCurrentState) name:UIApplicationDidEnterBackgroundNotification object:nil];
- - (void)dealloc
- {
- [[NSNotificationCenterdefaultCenter] removeObserver:self];
- }
- - (NSInteger)initialViewIndexForHorizontalScroller:(HorizontalScroller *)scroller
- {
- return currentAlbumIndex;
- }
- @interfaceAlbum : NSObject<NSCoding>
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:self.year forKey:@"year"];
- [aCoder encodeObject:self.title forKey:@"album"];
- [aCoder encodeObject:self.artist forKey:@"artist"];
- [aCoder encodeObject:self.coverUrl forKey:@"cover_url"];
- [aCoder encodeObject:self.genre forKey:@"genre"];
- }
- - (id)initWithCoder:(NSCoder *)aDecoder
- {
- self = [super init];
- if (self)
- {
- _year = [aDecoder decodeObjectForKey:@"year"];
- _title = [aDecoder decodeObjectForKey:@"album"];
- _artist = [aDecoder decodeObjectForKey:@"artist"];
- _coverUrl = [aDecoder decodeObjectForKey:@"cover_url"];
- _genre = [aDecoder decodeObjectForKey:@"genre"];
- }
- return self;
- }
- - (void)saveAlbums;
- - (void)saveAlbums
- {
- NSString *filename = [NSHomeDirectory() stringByAppendingString:@"/Documents/albums.bin"];
- NSData *data = [NSKeyedArchiverarchivedDataWithRootObject:albums];
- [data writeToFile:filename atomically:YES];
- }
- - (id)init
- {
- self = [super init];
- if (self) {
- NSData *data = [NSDatadataWithContentsOfFile:[NSHomeDirectory() stringByAppendingString:@"/Documents/albums.bin"]];
- albums = [NSKeyedUnarchiverunarchiveObjectWithData:data];
- if (albums == nil)
- {
- albums = [NSMutableArrayarrayWithArray:
- @[[[Album alloc] initWithTitle:@"Best of Bowie" artist:@"David Bowie" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_david%20bowie_best%20of%20bowie.png" year:@"1992"],
- [[Album alloc] initWithTitle:@"It‘s My Life" artist:@"No Doubt" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_no%20doubt_its%20my%20life%20%20bathwater.png" year:@"2003"],
- [[Album alloc] initWithTitle:@"Nothing Like The Sun" artist:@"Sting" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_sting_nothing%20like%20the%20sun.png" year:@"1999"],
- [[Album alloc] initWithTitle:@"Staring at the Sun" artist:@"U2" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_u2_staring%20at%20the%20sun.png" year:@"2000"],
- [[Album alloc] initWithTitle:@"American Pie" artist:@"Madonna" coverUrl:@"http://www.coversproject.com/static/thumbs/album/album_madonna_american%20pie.png" year:@"2000"]]];
- [self saveAlbums];
- }
- }
- return self;
- }
- - (void)saveAlbums;
- - (void)saveAlbums
- {
- [persistencyManager saveAlbums];
- }
- [[LibraryAPI sharedInstance] saveAlbums];
命令模式
- UIToolbar *toolbar;
- // We will use this array as a stack to push and pop operation for the undo option
- NSMutableArray *undoStack;
- toolbar = [[UIToolbar alloc] init];
- UIBarButtonItem *undoItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(undoAction)];
- undoItem.enabled = NO;
- UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
- UIBarButtonItem *delete = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteAlbum)];
- [toolbar setItems:@[undoItem,space,delete]];
- [self.view addSubview:toolbar];
- undoStack = [[NSMutableArrayalloc] init];
- - (void)viewWillLayoutSubviews
- {
- toolbar.frame = CGRectMake(0, self.view.frame.size.height-44, self.view.frame.size.width, 44);
- dataTable.frame = CGRectMake(0, 130, self.view.frame.size.width, self.view.frame.size.height - 200);
- }
- - (void)addAlbum:(Album*)album atIndex:(int)index
- {
- [[LibraryAPI sharedInstance] addAlbum:album atIndex:index];
- currentAlbumIndex = index;
- [self reloadScroller];
- }
- - (void)deleteAlbum
- {
- // 1
- Album *deletedAlbum = allAlbums[currentAlbumIndex];
- // 2
- NSMethodSignature *sig = [self methodSignatureForSelector:@selector(addAlbum:atIndex:)];
- NSInvocation *undoAction = [NSInvocationinvocationWithMethodSignature:sig];
- [undoAction setTarget:self];
- [undoAction setSelector:@selector(addAlbum:atIndex:)];
- [undoAction setArgument:&deletedAlbum atIndex:2];
- [undoAction setArgument:¤tAlbumIndex atIndex:3];
- [undoAction retainArguments];
- // 3
- [undoStack addObject:undoAction];
- // 4
- [[LibraryAPI sharedInstance] deleteAlbumAtIndex:currentAlbumIndex];
- [self reloadScroller];
- // 5
- [toolbar.items[0] setEnabled:YES];
- }
- - (void)undoAction
- {
- if (undoStack.count > 0)
- {
- NSInvocation *undoAction = [undoStack lastObject];
- [undoStack removeLastObject];
- [undoAction invoke];
- }
- if (undoStack.count == 0)
- {
- [toolbar.items[0] setEnabled:NO];
- }
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。