使用代理传递数据备份导航堆栈 [英] Using a delegate to pass data back up the navigation stack

查看:132
本文介绍了使用代理传递数据备份导航堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在两个视图控制器之间传递数据了两天,并且很困惑。我是Objective-C的新手,并找到一些棘手的东西让我头疼。

I have been battling with passing data between two view controllers for a couple of days now and getting very confused. I'm new to Objective-C and finding some parts tricky to get my head round.

我有一个导航控制器,FirstView是一个表单,在这个表单上我有一个加载SecondView的按钮,它包含一个TableView,供用户选择一些选项。然后我想将选择传递给FirstView控制器并显示数据等...

I have a Navigation Controller, FirstView is a form and on this form I have a button which loads SecondView which contains a TableView for the user to select some options. I then want to pass the selection back to the FirstView controller and display the data etc...

我已经阅读了很多关于这个(stackoverflow,iphonedevsdk,CS 193P资源)和我看到的选项,

I have read alot about this (stackoverflow, iphonedevsdk, CS 193P Resources) and the options i've seen are,

1)应用程序代理中的ivar(脏而不推荐)
2)创建一个单例
3 )创建一个数据模型类
4)使用协议和委托(由苹果推荐)

1) ivar in app delegate (dirty and not recommended) 2) create a singleton 3) create a data model class 4) Use protocols and delegates (recommended by apple)

我想做正确的事情,想使用选项4 - 代表在我的程序中

I want to do things right and want to use option 4 - Delegates in my program

问题是,我不明白代表以及如何设置和实现它们。

Problem is, I don't understand delegates and how to setup and implement them.

任何人都可以提供一个关于如何使用委托和2个视图控制器设置和传递NSArray的基本示例。

Could anyone provide a basic example on how to setup and pass an NSArray using the delegate and 2 view controllers.

提前感谢
Matt

Thanks in advance Matt

推荐答案

委托是在这种情况下使用的正确模式,但您的描述看起来不像授权一样使用全局变量。也许您正在应用代理中存储全局变量,您应该始终尽量避免。

Delegation is the correct pattern to be used in this case, but your description doesn't look much like delegation as it is using a global variable. Perhaps you're storing global variables in your App Delegate which you should always try to avoid.

以下是代码应如下所示的粗略概述:

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

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

@implementation FirstViewController

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

@end

这篇关于使用代理传递数据备份导航堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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