【问题标题】:Pushing data through UITableViewcell with unwind segues使用 unwind segues 通过 UITableViewcell 推送数据
【发布时间】:2015-01-02 15:28:59
【问题描述】:

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

所以现在,我尝试使用展开转场,所以现在之前输入的数据正在显示,但表格视图单元格没有像以前那样填充。

这是推送数据的代码-

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

这是展开转场的代码 -

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

【问题讨论】:

  • 与其在控制器之间创建依赖关系并尝试在它们之间传递数据,不如考虑创建一个单独的对象来代表您的应用程序的数据模型。当控制器想要更改某些内容时,请更新模型。当控制器需要显示某些内容时,请向模型询问其当前值。
  • 好的,你知道我该如何开始吗?

标签: ios xcode uitableview segue


【解决方案1】:

从 cmets 扩展,开始使用数据模型对象:

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

用法:

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

这假设您在数据模型中也有代码来设置配方数组。

【讨论】:

  • 它要求我添加“[”并且初始化元素不是编译时间常量
  • 信息仍然只是创建另一个视图并删除旧的东西。我认为这与推回原始控制器有关。
  • 如果您只将数据存储在模型中并在视图出现时从模型中更新,那么擦除显示的内容不再重要。 (我不知道您句子中的“信息”是什么意思。)但是,是的,您想放松,而不是创建重复的控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-18
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 2017-03-08
  • 2021-08-16
  • 2014-10-13
相关资源
最近更新 更多