How to pass information from the second ViewController to the first ViewController

3

I'm having trouble passing information from the second screen of a NavigationViewController to the first screen of NavigationViewController . On some gringos sites I found teaching to do by Protocol, but it is not working. Is there an easier and more practical way to do it?

Note: The first screen is a ViewController normal and the second is a TableViewController that should send the information of the selected cell to the first screen.

    
asked by anonymous 25.03.2014 / 03:05

1 answer

3

The direction you should take is precisely this one you have found, through protocol. It is very simple and this is independent whether it is TableViewController or not.

Let's say you have PrimeiraViewController and SegundaViewController . In this second, in your header file .h , we created the protocol (before the declaration @interface ):

@protocol SegundaViewDelegate <NSObject>
@required
- (void)foo:(id)bar;
@end

And in this same file, the property:

@property (nonatomic, retain) id<SegundaViewDelegate> segundaViewDelegate;

In the .m implementation file, wherever you pass this value, you call this method:

[self.segundaViewDelegate foo:[NSDate date]]

Here, I've just passed an example%, of course, of course, but you can create your method as and where you want, in your case it will be when a cell is pressed.

In NSDate , we add this delegate in .h :

@interface PrimeiraViewController : UIViewController <SegundaViewDelegate>

And when the second screen is triggered, we define this delegate :

SegundaViewController *viewController = [[SegundaViewController alloc] init];
[viewController setSegundaViewDelegate:self];

And we implemented the method that we will receive the information:

- (void)foo:(id)bar {
    NSLog("%@", bar);
}

See if you can understand and implement this way.

    
25.03.2014 / 12:55