AddCityViewController
将与其相应的文本字段一起还原。"取消"one_answers"保存"按钮包含对委托的调用。模式视图控制器正在恢复,但"保存"one_answers"取消"按钮未激活委托方法。所有视图控制器都已在情节提要中创建。
// AddCityViewController.h
@class City;
#import <UIKit/UIKit.h>
@protocol addCityDelegate;
@interface AddCityViewController : UIViewController
@property(nonatomic, weak) id <addCityDelegate> delegate;
@property(nonatomic,strong)NSManagedObjectContext *context;
@end
@protocol addCityDelegate
- (void)save: (City *)controller withBool:(BOOL )saveStatus;;
@end
"取消"按钮仅在未执行状态恢复时调用委托方法。
我希望代表在需要进行状态恢复时也能被呼叫
// AddCityViewController.m
- (IBAction)cancelButton:(UIBarButtonItem *)sender {
[self.delegate save:nil withBool:false];
}
#pragma mark - encodeRestorable and decodeRestorable
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.delegate forKey:@"restoreDelegate"];
[coder encodeObject:self.cityNameLabel.text
forKey:@"restoreCountyLabelText"];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.delegate forKey:@"restoreDelegate"];
_cityNameLabel.text = [coder decodeObjectForKey:@"restoreCountyLabelText"];
[super decodeRestorableStateWithCoder:coder];
}
CityTableViewController
是AddCityTableView
的委托
// CityTableViewController.m
#import "CityTableViewController.h"
#import "AddCityViewController.h"
#import "City.h"
#import "County.h"
@interface CityTableViewController ()<addCityDelegate>
@property(nonatomic,strong)NSFetchedResultsController *fetchedResultsController;
@end
#pragma mark - AddConjugations Delete
除了STATE RESTORATION之外,下面的委托方法运行良好。在状态恢复期间,从不调用此方法。
- (void)save: (AddCityViewController *)saveNewCity withBool:(BOOL )saveStatus
{
if (saveStatus) {
...
}
您试图再次对委托进行编码,而不是对其进行解码。
将代码更改为:
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder {
self.delegate = [coder decodeObjectForKey:@"restoreDelegate"];
_cityNameLabel.text = [coder decodeObjectForKey:@"restoreCountyLabelText"];
[super decodeRestorableStateWithCoder:coder];
}