【发布时间】:2010-08-08 01:18:55
【问题描述】:
我有一个 ItemAddViewController,它以模态视图的形式呈现自己。其中一个字段推送了一个新的视图控制器 CategorySelectionViewController,它允许用户选择单个类别。
ItemAddViewController.h
@property (nonatomic, retain) Category *category;
CategorySelectionViewController.h
@property (nonatomic, retain) Category *category;
CategorySelectionViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSManagedObject *currentCategory = category;
if (currentCategory != nil) {
NSInteger index = [categories indexOfObject:currentCategory];
NSIndexPath *selectionIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
UITableViewCell *checkedCell = [tableView cellForRowAtIndexPath:selectionIndexPath];
checkedCell.accessoryType = UITableViewCellAccessoryNone;
}
//set the checkmark accessory
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
//update the category
category =[categories objectAtIndex:indexPath.row];
NSLog(@"%@", category);
// Deselect row
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
ItemAddViewController.m
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"%@", category);
}
Category 在 CategorySelectionViewController 创建时设置。在类别选择屏幕上选择类别时,NSLog 会报告正确的对象。当它返回到 ItemAddViewController 时,它再次为空。这两个应该是同一个对象,所以我不确定我做错了什么。
基本上,我需要一种在两个视图控制器之间传递数据的好方法。
【问题讨论】: