使用 unwind segue 通过 UITableViewcell 推送数据 [英] Pushing data through UITableViewcell with unwind segues

查看:21
本文介绍了使用 unwind segue 通过 UITableViewcell 推送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 push segue,用于将数据从 uitableview 单元格传输到文本字段.应用程序计划是用户将数据输入文本字段,然后他们将单击一个按钮,它将模态切换到表视图控制器,然后他们将选择一个 uitableviewcell 并将其传输回原始视图他们将数据输入到文本字段中,并且 tableviewcell 内容将输入到同一视图上的不同文本字段中.但是我遇到了从原始文本字段中删除数据并创建另一个视图的问题.

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.

所以现在,我尝试使用 unwind segue,所以现在显示之前输入的数据,但表格视图单元格没有像以前那样填充.

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

这是 unwind segue 的代码 -

Here is the code for the unwind segue -

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

推荐答案

从评论展开,开始使用数据模型对象:

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

AppDataModel.h

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

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

用法:

[[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.

这篇关于使用 unwind segue 通过 UITableViewcell 推送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆