开发者

Passing data from popped view controller

开发者 https://www.devze.com 2023-03-23 03:28 出处:网络
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 a

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 objec开发者_如何学Pythont 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?

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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消