将数据从mainView传递到子视图 [英] pass data from mainView to a subview

查看:50
本文介绍了将数据从mainView传递到子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基于实用程序的应用程序,数据存储在 MainViewController 中,现在我知道如何将数据传递给 FlipsideViewController (对此有很多考虑)线程BTW,要将数据从Mainview发送到Flipside吗?).但是我正在将数据获取到我已添加到另一侧视图的子视图( UIView 的子类)中.如何将数据传递到此子视图?我看到在 FlipsideViewController.h 中已经建立了一个委托和协议,对于委托之类的事情我真的很陌生.任何帮助将不胜感激!

I am building a utility-based application, the data is stored in the MainViewController, and now I know how to pass data to the FlipsideViewController (many regards to this thread BTW, Sending data from Mainview to Flipside?). But I am getting the data onto an subview (subclass of UIView) that I have added to the flipside view. How can I pass data to this subview? I saw there is already a delegate and protocol set up in the FlipsideViewController.h, I am really new to the delegate sort of things. Any help would be greatly appreciated!

更新:

在主视图上,我有几个文本字段供用户输入以创建对象.所有对象都存储在一个数组中.即,我的数据已创建并存储在 MainViewController 中.现在,另一方面,我有一个自定义UIView子类,该子类允许我基于该数组中的数据进行自己的绘制.我在这里需要做的是将存储在 MainViewController 中的数据传递到此子视图.这是我的相关代码:

On the main view, I have a couple of text fields for users to input to create an object. All the objects are stored in an array. Namely, my data is created and stored in the MainViewController. Now on the flip side, I have a custom UIView subclass which allows me to do my own drawing based on the data in that array. What I need to do here is pass the data that stored in MainViewController to this subview. Here is my relevant code:

MainViewController.m

- (IBAction)showInfo:(id)sender {    

    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    controller.delegate = self;

    controller.receiver = data;//this is what I've done.

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];

    [controller release];
}

FlipsideViewController.h

@protocol FlipsideViewControllerDelegate;

@interface FlipsideViewController : UIViewController {
    id <FlipsideViewControllerDelegate> delegate;
    DataModel *receiver; //create a property to receive the data transferred from main view
}

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
@property (nonatomic, retain) DataModel *receiver;
- (IBAction)done:(id)sender;
@end


@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

在上面的代码中,数据"是在 MainViewController.h 文件中声明的DataModel对象.

In the above code, "data" is an DataModel object declared in the MainViewController.h file.

我想在 drawing 类(UIView的子类)中进行自定义绘制,如何将数据从 FlipsideViewController 传递到此子视图?我是否需要使用在 FlipsideViewController.h 文件中声明的委托?预先感谢!

And I want to do my custom drawing in drawing class (subclass of UIView), how can I pass data from the FlipsideViewControllerto this subview? Do I need to make use of delegate declared in the FlipsideViewController.h file? Thanks in advance!

推荐答案

我快速浏览了模板,并认为您对委托的用途感到困惑.

I have had a quick look at the template and think you are getting confused with what the delegate is being used for.

此模板中的委托未传输数据.单击完成按钮后,它会回调 MainViewController ,并要求它调用 dismissModalViewControllerAnimated 方法,以便它可以删除视图控制器.根据文档说明,这似乎有些多余

The delegate in this template is not transferring data. When you have clicked the done button it calls back to MainViewController and asks it to call the dismissModalViewControllerAnimated method so that it can remove the view controller. This seems a bit superflous as the documentation states

If you call this method on the modal view controller itself, however, the modal view controller automatically forwards the message to its parent view controller.

因此,您实际上不需要呼叫父级即可.

Therefore you don't really need to call the parent to do this.

在界面"构建器中,您可以看到 FlipsideView.xib File's Owner 设置为 FlipsideViewController.xib .

In Interface builder you can see that the FlipsideView.xib has it's File's Owner set to FlipsideViewController.xib.

现在,如果右键单击文件的所有者,您将看到 view 已连接到 View ,这基本上意味着 view FlipsideViewController 中属性的名称,而 View 是Interface Builder中的元素.

Now if you right click the File's Owner you will see that view is connected to View this basically means that view is the name of the property in FlipsideViewController and View is the element in Interface Builder.

因此,我们可以使用插座从 FlipsideViewController 访问xib文件中的元素.

Therefore we can access elements in the xib file from FlipsideViewController using outlets.

要说出标签,您需要做几件事

To say draw a label you will need to do a couple of things

首先在.h中添加一个属性,然后在.m中对其进行合成,例如

First add a property in the .h and synthesize it in the .m like

// FlipsideViewController.h
@interface FlipsideViewController : UIViewController

@property (nonatomic, retain) IBOutlet UILabel *testLabel; // <----- Added this
@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;

- (IBAction)done:(id)sender;

@end

// FlipsideViewController.m
@implementation FlipsideViewController

@synthesize delegate = _delegate;
@synthesize testLabel = _testLabel; // <----- Added this

// More methods

- (void)dealloc
{
   [_testLabel release];  // Always do you memory management
   [super dealloc];
}

然后返回Interface Builder

Then back in Interface Builder

  1. 在视图中添加 UILabel 元素
  2. ctrl +拖动文件的所有者到您添加的 UILabel
  3. 在我的示例中选择标签为 testLabel
  1. Add a UILabel element to your view
  2. ctrl + drag from File's Owner to the UILabel you added
  3. Select the label in my example it is testLabel

现在,这些已正确连接.您要在其中设置标签值的位置位于 viewDidLoad:中,您现在可以像这样

Now these are hooked up correctly. The place where you want to be setting the value of the label is in viewDidLoad: which you can now do like this

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.testLabel.text = @"It Works";  // You would use the data passed in from `MainViewController`
}

这篇关于将数据从mainView传递到子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆