简体   繁体   English

使用委托在视图之间传递数据

[英]Using delegate to pass data between views

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. 这里的一般概念是,我在主视图中创建并存储数据,然后使用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. 然后在FlipViewController中,我将数据存储在自己的委托中,并将其传递给实现我自己的委托方法的自定义视图。 The following is the main portions of the code. 以下是代码的主要部分。

MainViewController.m (only adopts <FlipsideViewControllerDelegate> protocol) 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;
}

It turns out the data didn't get passed to the chartData object in my custom view. 事实证明,数据没有传递到我的自定义视图中的chartData对象。 HELP? 救命?

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. FlipsideViewController类中,您定义了协议,该协议从本质上说如果您遵守此协议,则必须实现此方法。

How do you conform to a protocol? 您如何遵守协议?

In MainViewController the @interface will look something like this MainViewController@interface看起来像这样

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>

The fact that you have the protocol written in angled brackets means that you promise to conform to the protocol and therefore have to implement 您将协议写在尖括号中的事实意味着您承诺遵守协议,因此必须执行

 - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;

in your MainViewController.m . 在您的MainViewController.m

Now when MainNavigationController set's itself as the delegate ( controller.delegate = self; ) it finishes the link. 现在,当MainNavigationController将自身设置为委托( controller.delegate = self; )时,它将完成链接。 This allows the FlipsideViewController to call 这允许FlipsideViewController调用

[delegate flipsideViewControllerDidFinish:self];

Which will call the method defined in MainViewController which dismisses the modal view controller. 它将调用MainViewController中定义的方法,该方法将关闭模态视图控制器。

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 您已经定义了第二个协议(可以将方法添加到第一个协议中,然后不必采用两个协议),并且正如其他人指出的那样,您并未通过

controller.delegate2 = self;

This would not solve your problem. 这不会解决您的问题。 You would still need to conform to the ChartDelegate by adding it to the declaration. 您仍然需要通过将ChartDelegate添加到声明中来使其符合标准。 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.h

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>

- (IBAction)showInfo:(id)sender;

@end

MainViewController.m 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 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 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

You have two properties: delegate and delegate2. 您有两个属性:委托和委托2。 You are assigning a value to delegate, but calling the method on delegate2 later. 您正在为委托分配一个值,但是稍后在委托2上调用该方法。

You need to assign the delegate2 (your customDrawing class). 您需要分配delegate2 (您的customDrawing类)。 You are only assigning the delegate . 您只分配delegate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM