2011-08-21 29 views
-1

我正在构建一个实用程序应用程序,它在主视图和翻转视图之间共享数据。实际上,它并不完全是保存数据的翻转视图,它是自定义视图,当它被加载时,它是翻转视图的一个实例。我在前面的线索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对象在我的自定义视图。帮帮我?

+0

为什么不从您的视图抽象的模型出来,有两种观点观察模型?视图不应该存储数据... – Stephen

回答

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 
+0

您的解决方案似乎将数据提供给flipsideview,正如我之前所说的,这不是我需要做的。我需要使用从mainview传递的数据在flipsideview顶部的子视图上进行自定义绘制。这就是为什么在flipsideview加载后我立即将“chart”传递给viewDidLoad方法中的delegate2。 – Michael

+0

谢谢!我已经完成了。我忘了在flipsideviewcontroller中实例化一个对象,现在我只需要直接将数据传递给该子视图对象。 – Michael

0

您有两个属性:delegate和delegate2。您正在为委托分配值,但稍后会在委托人2上调用该方法。

+0

你能告诉我如何解决这个问题吗?我真的是新的代表事物... – Michael

0

您需要指定delegate2(您的customDrawing类)。您只分配delegate

+0

你的意思是我需要controller.delegate2 = self;在我的mainviewcontroller.m?这意味着我必须在mainviewcontroller中将一起使用? – Michael

+0

您需要指定'delegate2'的任何类,即保存处理方法的实现。也许主视图控制器是一个好地方。 – Mundi

+0

我在我的customDrawing类的方法中分配了delegate2,如下所示:ChartDelegate * chartDelegate = [[ChartDelegate alloc] initWithNibName:@“FlipsideView”bundle:nil]; chartDelegate.delegate2 = self;但有一个错误说“ChartDelegate”未申报......我错过了什么?我在mainviewcontroller中检查了showInfo(),它的工作原理与此相同。 – Michael

相关问题