gpt4 book ai didi

ios - 将数据解析回 View Controller

转载 作者:行者123 更新时间:2023-11-29 03:00:55 24 4
gpt4 key购买 nike

我是 ios 编程新手;我编写了一个应用程序以在 UITableView 中显示食谱列表,并在选择一行后,在详细 View 中将行标题解析为 UITextField

问题:我该怎么做才能将详细 View 中的标题更改保存到表格 View (我使用 Storyboard)?

这是我的代码:

NViewController.m:

- (void)viewDidLoad
{
[super viewDidLoad];

recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", nil];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"RecipeCell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.lblName.text = [recipes objectAtIndex:indexPath.row];
return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
NDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedRowTitle = [recipes objectAtIndex:indexPath.row];
}
}

NDetailViewController.m:

@interface NDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextField *txtDetailText;

@end

@implementation NDetailViewController

@synthesize selectedRowTitle;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization

}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

self.txtDetailText.text = selectedRowTitle;
}

最佳答案

在NViewController中,添加NSIndexPath来保存:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = [self._tableView indexPathForSelectedRow];
NDetailViewController *destViewController = segue.destinationViewController;
destViewController.selectedRowTitle = [recipes objectAtIndex:indexPath.row];

// need to save this indexPath to update when textfield is changed. Because indexPath maybe change if data changes, I think you should choose another field (like KEY, distingue objects)
destViewController.indexPath = indexPath;
destViewController.delegate = self;
}
}

-(void)changeTextFieldWithIndexPath : (NSIndexPath*)indexPath content:(NSString*)content;
{
// you update indexPath to row, and reloadTable
}

NDetailViewController.h:

@protocol NDetailViewControllerDelegate <NSObject>
-(void)changeTextFieldWithIndexPath : (NSIndexPath*)indexPath content:(NSString*)content;
@end

@interface NDetailViewController : ViewController
@property (nonatomic, weak) id<NDetailViewControllerDelegate> delegate;
@end

NDetailViewController.m:当textfiled改变值时,我的方法只是一个例子:

- (void) whenContentOfTextFieldChanged
{
[self.delegate changeTextFieldWithIndexPath:indexPath content:@"content which textfield is changed"];
}

关于ios - 将数据解析回 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23310352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com