ChewOnThis_Trident
ChewOnThis_Trident

Reputation: 2259

Return for a push segue

I am new to story boards and I am trying to move between table view controllers.

I understand how to set up a segue and how to send data to the new story board but my question is returning data back. If I use the push style segue it gives me an automatic back button. I want a page that will create a "job" and save it if they hit save (a bar button I created on the other side of the title) When I set up the segue to go back to the main page from the save button it made that main table view controller a child (instead of simply going back to it's original state). The work around I was thinking was saving it to a file when they hit save and whenever they load the main table view it loads from that file. Is this a common and correct way to do this or should I be trying to return that object and save it in the main table view controller?

Upvotes: 0

Views: 1102

Answers (2)

FreeNickname
FreeNickname

Reputation: 7764

A common approach is to use delegation. Since ViewControllers should be as independent as it is possible, we have to minimize dependencies between them. And actually your idea with the file, if I understand it correctly, does it as well. But using files to organize a communication between ViewControllers is not very convinient. Usually you declare a @protocol SecondViewControllerDelegate for your second ViewController (where you click a "Save" button). With methods like:

@protocol YourSecondViewControllerDelegate <NSObject>
-(void)yourSecondViewControllerDidCancel:(YourSecondViewController*)controller; //if you have a cancel button
-(void)yourSecondViewControllerDidFinish:(YourSecondViewController*)controller yourDataToReturn:(SomeData*)data andSomeMoreData:(AnotherDataType*)data2;
@end

Then you add a property like this to your SecondViewController:

@property (nonatomic, assign) id<YourSecondViewControllerDelegate> delegate; //Do not retain it!

Then you adopt your protocol in your MainViewController.

@interface MainViewController()<YourSecondViewControllerDelegate> //you can do it in the private category to keep the class interface clear.
@end
@implementation
//don't forget to implement those methods here :)
@end

And when you trigger your segue from the MainViewController, you can set your MainViewController as a delegate for the SecondViewController:

SecondViewController *destinationController = [[SecondViewController alloc] init]; //Just for example.
destinationController.delegate = self;
//trigger a segue :)

When the user presses the Save button in the SecondViewController, you call the delegate's yourSecondViewControllerDidFinish:

[self.delegate yourSecondViewControllerDidFinish: self yourDataToReturn: someData andSomeMoreDate: someMoreData];

This way MainController's yourSecondViewControllerDidFinish method will be called. And you can pick someData and someMoreData up there.

You can get more details about it in this tutorial: Apple's your second iOS app tutorial

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

If you're making a segue to go "back" to a controller, then that segue needs to be an unwind segue. An unwind segue goes back to the same instance of the controller that you came from originally. If you need to send data back to that controller, you can do it in prepareForSegue.

Upvotes: 1

Related Questions