我正在构建一个实用程序应用程序,它在主视图和翻转视图之间共享数据。实际上,它并不是持有数据的翻转视图,它是自定义视图,它是加载时翻转视图的一个实例。我已经解释了我之前的帖子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对象。 HELP?
答案 0 :(得分:5)
你错过了基本面。我认为你不需要代表来完成这项任务,但我们走了。
协议就像合同。在您FlipsideViewController
类中,您定义了基本上表明如果符合此协议的协议,那么您必须实现此方法。
您如何遵守协议?
在MainViewController
@interface
看起来像这样
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
您将协议写入有角度的括号表示您承诺遵守协议,因此必须实施
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
在MainViewController.m
。
现在,当MainNavigationController
将自己设置为委托(controller.delegate = self;
)时,它就会完成链接。这允许FlipsideViewController
调用
[delegate flipsideViewControllerDidFinish:self];
这将调用MainViewController
中定义的方法来解除模态视图控制器。
您已经定义了第二个协议(您可以将方法添加到第一个协议,然后您不必采用两个协议),而其他人已经指出您没有通过执行
来链接这些类controller.delegate2 = self;
这不会解决您的问题。您仍然需要通过将其添加到声明来符合ChartDelegate
。一旦你做完了,你仍然不会没有水,因为方法不正确。
完整解决方案 - 不使用代理,因为这里并不真正需要
MainViewController.h
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
- (IBAction)showInfo:(id)sender;
@end
MainViewController.m
@implementation MainViewController
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo:(id)sender
{
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.delegate = self;
/*
* The labelText property is defined in the header for FlipsideViewController
* In this case this is the easiest way to get data from this controller to
* the controller we are about to display
*/
controller.labelText = @"WHAT EVER YOU WANT TO SEND"; // <---- sending data
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
FlipsideViewController.h
@class FlipsideViewController;
@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end
@interface FlipsideViewController : UIViewController
/*
* These properties have been added. The label is used for displaying the text
* and needs to be hooked up in Interface builder
*
* The NSString is the property that is holding the data passed from MainViewController
*/
@property (nonatomic, retain) IBOutlet UILabel *testLabel;
@property (nonatomic, copy) NSString *labelText; from MainViewControlller
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
- (IBAction)done:(id)sender;
@end
FlipsideViewController.m
@implementation FlipsideViewController
@synthesize delegate = _delegate;
/*
* We need to synthesise out properties so we get our getters and setters created
*/
@synthesize testLabel = _testLabel;
@synthesize labelText = _labelText;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
/*
* This is called once the view is set up and all connections have been made in
* interface builder. Therefore we can now set the text of our test label
*/
self.testLabel.text = labelText;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Actions
- (IBAction)done:(id)sender
{
[self.delegate flipsideViewControllerDidFinish:self];
}
- (void)dealloc
{
/*
* Memory management for the ivars we added
*/
[_testLabel release];
[_labelText release];
[super dealloc];
}
@end
答案 1 :(得分:0)
您有两个属性:delegate和delegate2。您正在为委托分配值,但稍后在delegate2上调用该方法。
答案 2 :(得分:0)
您需要指定delegate2
(您的customDrawing
班级)。您只是分配delegate
。