从弹出的视图控制器传递数据 [英] Passing data from popped view controller

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

问题描述

我有两个视图控制器.我在第一个,当我按下按钮时,第二个视图控制器被推到导航控制器的堆栈上.在这里,在第二个视图控制器中,我有一个表视图,当我点击某些行时,它们被选中(如复选框),并且与该行相关的一些数据被添加到数组中.现在,当我完成选择后,我想回到第一个视图控制器并使用该数组.怎么做?现在我的应用程序是这样工作的:我有一个委托协议,然后是我有属性数组的对象,我可以从整个应用程序访问该对象及其数组......但我真的不喜欢那样.这是正确/最好/最简单的方法吗?

I have two view controllers. I'm on first, and when I press the button, second view controller is pushed onto the stack of navigation controller. Here, in second view controller I have a table view and when I tap on some rows, they are selected (like checkboxes) and some data related to that rows are added to an array. Now when I'm done with selecting, I want to go back to the first view controller and use that array. How to do that? Now my app works like this: I have a delegation protocol, then object in which I have the property array, and I can access that object and its array from whole app...but I don't really like that. Is this correct/best/simplest way to do that?

推荐答案

我有一个委托协议,然后是我拥有属性数组的对象,我可以从整个应用程序访问该对象及其数组......但我真的不喜欢那样.这是正确/最好/最简单的方法吗?

I have a delegation protocol, then object in which I have the property array, and I can access that object and its array from whole app...but I dont really like that. Is this correct/best/simplest way to do that?

委托是此处使用的正确模式,但您所描述的与其说是委托,不如说是使用全局变量.也许您将全局变量存储在您的 App Delegate 中——通常是您可以避免的情况.

Delegation is the correct pattern to use here, but what you describe isn't so much delegation as it is using a global variable. Perhaps you're storing globals in your App Delegate -- generally something you can avoid if you can.

以下是代码外观的粗略概述:

Here's a rough outline of what the code should look like:

SecondViewController.h:

SecondViewController.h:

@protocol SecondViewControllerDelegate;

@interface SecondViewController;

SecondViewController : UIViewController
{
    id<SecondViewControllerDelegate> delegate;

    NSArray* someArray;
}

@property (nonatomic, assign) id<SecondViewControllerDelegate> delegate;
@property (nonatomic, retain) NSArray* someArray;

@end

@protocol SecondViewControllerDelegate
- (void)secondViewControllerDidFinish:(SecondViewController*)secondViewController;
@end

SecondViewController.m:

SecondViewController.m:

@implementation SecondViewController

@synthesize delegate;
@synthesize someArray;

- (void)dealloc
{
    [someArray release];
    [super dealloc];
}

- (void)someMethodCalledWhenUserIsDone
{
    [delegate secondViewControllerDidFinish:self];
}

FirstViewController.h:

FirstViewController.h:

#import SecondViewController

@interface FirstViewController : UIViewController <SecondViewControllerDelegate>
{
    ...
}

@end

FirstViewController.m:

FirstViewController.m:

@implementation FirstViewController

- (void)secondViewControllerDidFinish:(SecondViewController*)secondViewController
{
    NSArray* someArray = secondViewController.someArray
    // Do something with the array
}

@end

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

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