我正在构建一个实用程序应用程序,它在主视图和翻转视图之间共享数据。实际上,它并不完全是保存数据的翻转视图,它是自定义视图,当它被加载时,它是翻转视图的一个实例。我在前面的线索here中解释了具体细节,但我还没有解决方案。我已经重新开发了我的代码,希望这次我可以说清楚。使用代理在视图之间传递数据
这里的一般概念是我在我的主视图中创建和存储数据,并使用FlipViewController中的预定义委托将其传递到另一侧视图。然后在FlipViewController中,我将数据存储在自己的委托中,并将其传递给实现我自己的委托方法的自定义视图。以下是代码的主要部分。
MainViewController.m
(仅采用<FlipsideViewControllerDelegate>
协议)
- (IBAction)showInfo:(id)sender {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.delegate = self;
controller.chart = data;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
FlipsideViewController.h
@protocol FlipsideViewControllerDelegate;
@protocol ChartDelegate;
@interface FlipsideViewController : UIViewController {
id <FlipsideViewControllerDelegate> delegate;
id <ChartDelegate> delegate2;
DataModel *chart;
}
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
@property (nonatomic, assign) id <ChartDelegate> delegate2;
@property (nonatomic, retain) DataModel *chart;
- (IBAction)done:(id)sender;
@end
@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end
@protocol ChartDelegate <NSObject>
- (void)getParams:(DataModel *)dataModel;
@end
FlipsideViewController.m
@synthesize delegate, delegate2;
@synthesize chart;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
if ([delegate2 respondsToSelector:@selector(getParams:)]) {
[delegate2 getParams:chart];
}
}
customDrawing.h
@interface customDrawing : UIView <ChartDelegate>{
DataModel *chartData;
}
@property (nonatomic, retain) DataModel *chartData;
@end
customDrawing.m
@synthesize chartData;
-(void)getParams:(DataModel *)dataModel{
chartData = dataModel;
}
原来的数据没有得到传递给chartData对象在我的自定义视图。帮帮我?
为什么不从您的视图抽象的模型出来,有两种观点观察模型?视图不应该存储数据... – Stephen