Kets
Kets

Reputation: 468

Get data back to first ViewController

I'm finding some difficulties here. I'm setting up an UITableView where users can add stuff to. I'm done with the AddViewController, where users can get to by pressing the + on the UIBarNavigationButton, and then Push Segue to my AddViewController.

Here in my AddViewController is an Array where the user's data is stored in. Now how do I get the data in my UITableView in my FirstViewController array? I was thinking with another Push Segue from the Save UIBarNavigationButton and pushing the User array into the TableView Data?

Is this the right way to do it?

Kind Regards

Upvotes: 0

Views: 88

Answers (4)

user3153627
user3153627

Reputation: 1385

For transforming data from B ViewController to A ViewController, you can use Unwind Segue, Protocols and Delegate and also Blocks. Here i am showing you how to transform data using Protocol & Delegate.

In B VC, in H file add

1) Just above @interface:

@protocol addDataProtocol <NSObject>

-(void)addData:(NSArray *)arrayData;

@end

2) dd property

@property(nonatomic,retain)id<addDataProtocol>delegate;

3) In the M file, just before [self.navigationController popViewControllerAnimated:YES]; add:

[self.delegate addData:yourArray];

4) In A VC:

confirm the delegate before pushing, like : objBViewController.delegate=self;

5) Call the method:

-(void)addData:(NSArray *)arrayFromB

and access arrayFromB.

Upvotes: 1

Zack
Zack

Reputation: 871

Following along with what Tander said, you should use an unwind segue not push. Follow this example as a general guideline.

Use a similar setup like this in your FirstViewController:

//FirstViewController.m

- (IBAction)setArrayFromPreviousController:(UIStoryboardSegue *)segue
{
    AddViewController *controller = [segue sourceViewController];

    //old array and new array are the two array variable names
    [self setOld_array:[controller new_array]];
}

Alternatively, you could use a protocol method along with a delegate. This is a little bit more complex:

A great tutorial on how to accomplish this can be found here.

Upvotes: 2

Marcal
Marcal

Reputation: 1371

On your TVC you have to create an action which accepts a UIStoryboardSegue (I think). On you AddVC you can ctrl-drag the exit button to the little green icon on the storyboard scene. You then conect one with another. All you have left to do is to implement prepareForSegue on AddVC so you can pass your array back to the TVC. BTW, the action on TVC should also accept the array you're passing.

You can do a search on youtube, there must be a bazilion of tutorials about it.

Upvotes: 0

Robert J. Clegg
Robert J. Clegg

Reputation: 7370

If you're asking if you should use a push segue to move data back to previous controller, the answer is no.

What you can do is use unwind segues for this:

https://developer.apple.com/library/ios/technotes/tn2298/_index.html

Upvotes: 1

Related Questions