从弹出视图控制器传递数据

发布于 2024-11-26 10:36:14 字数 243 浏览 3 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫轩蝶泪 2024-12-03 10:36:14

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

委派是此处使用的正确模式,但您所描述的并不是委派,而是使用全局变量。也许您正在将全局变量存储在您的应用程序委托中——如果可以的话,通常您可以避免这种情况。

以下是代码的大致轮廓:

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:

@implementation SecondViewController

@synthesize delegate;
@synthesize someArray;

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

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

FirstViewController.h:

#import SecondViewController

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

@end

FirstViewController.m:

@implementation FirstViewController

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

@end

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?

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:

@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:

@implementation SecondViewController

@synthesize delegate;
@synthesize someArray;

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

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

FirstViewController.h:

#import SecondViewController

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

@end

FirstViewController.m:

@implementation FirstViewController

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

@end
爱要勇敢去追 2024-12-03 10:36:14

您需要引用您的secondViewController,并为其创建一个对象。

secondViewController *object2 = [[SecondViewController alloc] init];

object2.thatArray 将包含数组的内容。确保当您离开该视图控制器时该数组保留其值(或者您可以在 AppDelegate 中创建该数组,以便所有视图控制器都可以访问它)。

You need to reference your secondViewController, and create an object for it.

secondViewController *object2 = [[SecondViewController alloc] init];

object2.thatArray would have the contents of the array. Make sure that the array retains it's values when you leave that view controller (or you can create that array in your AppDelegate so that it can be accessed by all viewControllers).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文