matt suff
matt suff

Reputation: 27

Pushing data through UITableViewcell with unwind segues

I have a push segue that i use to transfer data from a uitableview cell to text fields. The app plan is that the user will enter data into a text field, then they will click on a button and it will modal segue over to a table view controller, then they will select a uitableviewcell and it will be transferred back to the original view where they had entered the data into the text field and the tableviewcell content will be entered into different text fields on the same view. But i am experiencing issues where the data is being erased from the original text field and it is creating another view.

So now, i tried using an unwind segue so now the data previously being entered is showing up but the table view cell is not populating like it did before.

Here is the code to push the data -

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = nil;
        Recipe *recipe = nil;
        if (self.searchDisplayController.active) {
            indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
            recipe = [searchResults objectAtIndex:indexPath.row];
        } else {
            indexPath = [self.tableView indexPathForSelectedRow];
            recipe = [recipes objectAtIndex:indexPath.row];
        }


PersonDetailTVC *destViewController = segue.destinationViewController;
        destViewController.recipe = recipe;

        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

Here is the code for the unwind segue -

- (IBAction)unwindFromDetailViewController:(UIStoryboardSegue *)segue
{
    // ViewController *detailViewController = [segue sourceViewController];
    NSLog(@"%@", segue.identifier);
}

Upvotes: 0

Views: 206

Answers (1)

Phillip Mills
Phillip Mills

Reputation: 31016

Expanded from comments, getting started with a data model object:

AppDataModel.h

#import <Foundation/Foundation.h>

@class Recipe;

@interface AppDataModel : NSObject

+ (instancetype)sharedData;

- (void)selectRecipeAt:(NSUInteger)index;

@property (nonatomic, strong, readonly) Recipe *selectedRecipe;

@end

AppDataModel.m

#import "AppDataModel.h"

@interface AppDataModel ()

@property (nonatomic, strong) NSArray *recipes;
@property (nonatomic, strong, readwrite) Recipe *selectedRecipe;

@end

@implementation AppDataModel

+ (instancetype)sharedData {
    static AppDataModel *dataModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dataModel = [[AppDataModel alloc] init];
    });
    return dataModel;
}

- (void)selectRecipeAt:(NSUInteger)index {
    self.selectedRecipe = [self.recipes objectAtIndex:index];
}

@end

Usage:

[[AppDataModel sharedData] selectRecipeAt:0];
Recipe *toDisplay = [[AppDataModel sharedData] selectedRecipe];

This assumes that you also have code in the data model to set up the array of recipes.

Upvotes: 0

Related Questions