Pushing data through UITableViewcell with unwind segues

我与影子孤独终老i 提交于 2019-12-12 03:07:35

问题


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);
}

回答1:


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.



来源:https://stackoverflow.com/questions/27744454/pushing-data-through-uitableviewcell-with-unwind-segues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!