使用委托将数据传回导航堆栈 [英] Using a delegate to pass data back up the navigation stack

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

问题描述

几天来,我一直在与在两个视图控制器之间传递数据作斗争,并且感到非常困惑.我是 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.

提前致谢马特

推荐答案

委托是在这种情况下使用的正确模式,但您的描述看起来不像委托,因为它使用全局变量.也许你在你的 App Delegate 中存储了全局变量,你应该尽量避免.

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:

@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天全站免登陆