从popup的视图控制器传递数据
我有两个视图控制器。 我第一次,当我按下button,第二个视图控制器被推到导航控制器的堆栈上。 在这里,在第二个视图控制器我有一个表格视图,当我点击一些行,他们被选中(如checkbox),并与该行相关的一些数据被添加到数组。 现在,当我完成select,我想回到第一个视图控制器,并使用该数组。 怎么做? 现在我的应用程序是这样工作的:我有一个委托协议,然后是我有属性数组的对象,我可以从整个应用程序访问该对象和它的数组… …但我不喜欢那样。 这是正确/最好/最简单的方法来做到这一点?
我有一个委托协议,然后我有属性数组的对象,我可以从整个应用程序访问该对象和它的数组… …但我不真的那样。 这是正确/最好/最简单的方法来做到这一点?
授权是在这里使用的正确模式,但是您所描述的不是使用全局variables的委托。 也许你在你的App Delegate中存储全局variables – 通常你可以避免的事情。
下面是代码应该看起来像一个粗略的轮廓:
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
你需要reference
你的secondViewController
,并为它创build一个对象。
secondViewController *object2 = [[SecondViewController alloc] init];
object2.thatArray
将有数组的内容。 当你离开视图控制器时,确保数组保留它的值(或者你可以在你的AppDelegate
创build这个数组,使得它可以被所有viewController访问)。