将数据从mainView传递到子视图

时间:2011-08-21 07:14:53

标签: iphone uiapplicationdelegate

我正在构建一个基于实用程序的应用程序,数据存储在MainViewController中,现在我知道如何将数据传递给FlipsideViewController(很多关于这个线程的问题BTW,{{3 }})。但我将数据放到我已添加到翻转视图的子视图(UIView的子类)中。如何将数据传递到此子视图?我看到FlipsideViewController.h中已经设置了一个委托和协议,我对代表的事情真的很新。任何帮助将不胜感激!


更新

在主视图中,我有几个文本字段供用户输入以创建对象。所有对象都存储在一个数组中。即,我的数据被创建并存储在MainViewController中。现在另外,我有一个自定义的UIView子类,它允许我根据该数组中的数据自己绘制。我需要做的是将存储在MainViewController中的数据传递给此子视图。这是我的相关代码:

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

在上面的代码中,“data”是在MainViewController.h文件中声明的DataModel对象。

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

2 个答案:

答案 0 :(得分:1)

我快速浏览了一下模板,并认为你对代表的用途感到困惑。

此模板中的委托不传输数据。单击完成按钮后,它会回调MainViewController并要求它调用dismissModalViewControllerAnimated方法,以便它可以删除视图控制器。这似乎有点夸张,因为文档陈述

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.

因此,你真的不需要打电话给父母来做这件事。

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

enter image description here

现在,如果您右键单击File's Owner,您会看到view已连接到View,这基本上意味着viewFlipsideViewController中的属性名称}和View是Interface Builder中的元素。

enter image description here

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

说要画一个标签你需要做几件事

首先在.h中添加一个属性,然后在.m中合成它

// 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

  1. 在您的视图中添加UILabel元素
  2. ctrl + dragFile's Owner到您添加的UILabel
  3. 在我的示例中选择标签为testLabel
  4. 现在这些都正确连接了。您想要设置标签值的地方位于viewDidLoad:,您现在可以这样做

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

答案 1 :(得分:0)

我发现将数据从一个视图传递到另一个视图的最简单方法是直接在原始视图的下一个视图中设置数据。

例如;

在FlipsideViewController.h中,为要传递的数据声明一个“容器”。双方必须是同一班级才能正常工作,即。 NSArray到NSArray,NSMutableDictionary到NSMutableDictionary。

NSMutableArray *newData;
...
@property (nonatomic, retain) NSMutableArray *newData; // This allows you to access this object from outside this class.

和FlipsideViewController.m

@synthesize newData;
...
[newData release];

现在我们需要传递数据,可以这么说。假设我们想要“发送”的数据存储在名为“results”的NSMutableArray中。

在我们的MainViewController.m中,当我们实例化我们的下一个视图控制器(在本例中为FlipsideViewController)时,我们可以在我们初始化nib之后直接引用newData可变数组。

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.newData = results;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];

确保在MainViewController.h文件中导入FlipsideViewController。

如果在.h文件中声明属性,则可以从视图堆栈中的任何位置引用该对象的内容!

希望有所帮助:D