使用委托在视图之间传递数据
我正在构建一个实用程序应用程序,它在主视图和翻转视图之间共享数据。实际上,保存数据的并不完全是翻转视图,而是加载时作为翻转视图实例的自定义视图。我已经在之前的帖子此处解释了具体细节,但我还没有找到解决方案。我重新开发了我的代码,希望这次我能说清楚。
这里的一般概念是我在主视图中创建和存储数据,并使用 FlipViewController 中的预定义委托将其传递到翻转视图。然后在 FlipViewController 中,我将数据存储在我自己的委托中,并将其传递给实现我自己的委托方法的自定义视图。以下是代码的主要部分。
MainViewController.m
(仅采用
协议)
- (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 对象。帮助?
I am building a utility application which shares data between main view and flip view. Actually, it is not exactly the flip view that's holding data, it's the custom view that's an instance of the flip view when it gets loaded. I have explained the specifics in my previous thread here, but I haven't got a solution yet. And I have redeveloped my code, hopefully this time I could make myself clear.
The general concept here is I create and store data in my main view, and pass it to the flip side view using the predefined delegate in the FlipViewController. Then in the FlipViewController, I store the data in my own delegate and pass it to the custom view which implements my own delegate method. The following is the main portions of the code.
MainViewController.m
(only adopts <FlipsideViewControllerDelegate>
protocol)
- (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;
}
It turns out the data didn't get passed to the chartData object in my custom view. HELP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你缺少基础知识。我认为您不需要代表来完成这项任务,但我们开始吧。
协议就像一份合同。在您的
FlipsideViewController
类中,您定义了协议,该协议本质上表明如果您符合此协议,则必须实现此方法。如何遵守协议?
在
MainViewController
中,@interface
看起来像这样事实上,您将协议写在尖括号中意味着您承诺遵守协议,因此必须
在
MainViewController.m
中实现。现在,当 MainNavigationController 将自身设置为委托 (
controller.delegate = self;
) 时,它会完成链接。这允许 FlipsideViewController 调用Which 将调用 MainViewController 中定义的方法,该方法会关闭模态视图控制器。
您已经定义了第二个协议(您可以将该方法添加到第一个协议中,然后您不必采用两个协议),并且正如其他人指出的那样,您没有通过这样做来链接这些类
这不会解决您的问题。您仍然需要通过将 ChartDelegate 添加到声明中来遵守它。一旦你这样做了,你仍然无法脱离水面,因为方法不正确。
完整解决方案 - 不使用委托,因为这里并不真正需要它们
MainViewController.h
MainViewController.m
FlipsideViewController.h
FlipsideViewController.m
You are missing the fundamentals. I do not think you need delegates to achieve this task but here we go.
A protocol is like a contract. In you
FlipsideViewController
class you defined the protocol which essentially states if you conform to this protocol then you must implement this method.How do you conform to a protocol?
In
MainViewController
the@interface
will look something like thisThe fact that you have the protocol written in angled brackets means that you promise to conform to the protocol and therefore have to implement
in your
MainViewController.m
.Now when
MainNavigationController
set's itself as the delegate (controller.delegate = self;
) it finishes the link. This allows theFlipsideViewController
to callWhich will call the method defined in
MainViewController
which dismisses the modal view controller.You have defined a second protocol (you could have added the method to the first and then you would not have to adopt two protocols) and as others have pointed out you have not linked the classes up by doing
This would not solve your problem. You would still need to conform to the
ChartDelegate
by adding it to the declaration. Once you have done that you will still not be out of the water because the method is not correct.Full solution - not using delegates as they are not really required here
MainViewController.h
MainViewController.m
FlipsideViewController.h
FlipsideViewController.m
您有两个属性:delegate 和 delegate2。您正在为 delegate 分配一个值,但稍后调用 delegate2 上的方法。
You have two properties: delegate and delegate2. You are assigning a value to delegate, but calling the method on delegate2 later.
您需要分配
delegate2
(您的customDrawing
类)。您仅分配委托
。You need to assign the
delegate2
(yourcustomDrawing
class). You are only assigning thedelegate
.