命令行設計模式:
命令設計模式將一個請求或行動作封裝為對象。這個封裝請求比原始的請求要靈活並且可以在對象之前被傳遞,存儲,動態修改或者放進隊列里面。蘋果
蘋果公司實現這種模式使用Target-Action機制和Invocation。
你可以閱讀更多關於Target-Action機制在蘋果的文檔但是Invocation類可以包含一個目標對象,方法選擇器和一些參數。當需要的時候這個對象可以被動態的改變。
這是一個完美的命令模式的例子。它將發送對象從接收對象和對象和可以持續請求或一連串的請求。
怎么使用命令設計模式?
在你進入invocation 操作之前。你需要設置framework來支持撤銷操作。你必須定義一個UIToolBar 和NSMutableArray 來作為撤銷棧。
在Viewcontroller實現文件延展里添加其他的實例變量:
UIToolbar *toolbar;
// We will use this array as a stack to push and pop operation for the undo option
NSMutableArray *undoStack;
創建一個toolbar 來展示按鈕和新的操作。以及一個數組作為命令隊列。
添加下面代碼在ViewDidLoad:
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 = [[NSMutableArray alloc] init];
上面代碼創建一個toolbar帶兩個按鈕和在他們之間添加一個靈活的空間。它還創建一個空撤銷堆棧。這里的撤銷按鈕被禁用,因為撤銷堆棧開始空了。
注意,工具欄沒有用坐標初始化,由於在ViewDidLoad設置的坐標大小不是最終的。因此最后的設置通過下面代碼:
- (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); }
您將添加三個ViewController方法。m處理專輯管理操作:添加、刪除和撤銷。
第一個方法用來添加新專輯
- (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 = [NSInvocation invocationWithMethodSignature: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]; }
對上面做下解釋:
1:讓這張專輯刪除
2:定義一個NSMethodSignature 類型為了創建NSInvocation。為了撤銷刪除操作。這個
NSInvocation需要知道三個事情:選擇器(什么消息被發送),目標(誰發送這個消息)還有消息的參數。這個例子中一旦你撤銷刪除
他消息發送刪除的相反您需要添加刪除專輯。
3:撤銷操作被創建后添加到空棧里面。這個操作被調價到數組的組后,就像正常的棧。
4:
用libraryAPI來刪除專輯從數據架構中並且加載滑動視圖。
5:自撤銷堆棧的操作,您需要啟用撤銷按鈕
注意:對於NSInvocation你需要注意下面幾點:
1:參數必須通過指針傳遞
2:參數從2的索引開始,指數0和1都保留在目標和選擇器。
3:果有機會,參數將被收回,那么你應該叫retainArguments。
最后添加撤銷操作:
- (void)undoAction { if (undoStack.count > 0) { NSInvocation *undoAction = [undoStack lastObject]; [undoStack removeLastObject]; [undoAction invoke]; } if (undoStack.count == 0) { [toolbar.items[0] setEnabled:NO]; }}
撤消操作“彈出”中的最后一個對象棧。這個對象總是NSInvocation類型,可以通過調用調用…調用。這張專輯刪除時調用的命令很早被創建。並添加刪除專輯專輯列表。因為你也刪除最后一個對象在堆棧“彈出”時,你現在看看棧是空的。如果是,這就意味着沒有更多的行動取消。所以你禁用撤銷按鈕。構建和運行您的應用程序來測試你的撤銷機制,刪除一個專輯(或兩個),點擊取消按鈕看它的實際應用:
這也是一個不錯的測試是否保留更改相冊數據之間的會話。現在,如果你刪除一個專輯,將應用程序發送到后台,然后終止應用程序,下次啟動應用程序顯示專輯列表應該反映刪除。

