我有一個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];
}
ItemAddViewCo ntroller.m
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"%@", category);
}
類別在CategorySelectionViewController創建上設置。當在類別選擇屏幕上選擇類別時,NSLog報告正確的對象。當它返回到ItemAddViewController時,它再次爲空。這兩個應該是同一個對象,所以我不確定我做錯了什麼。
基本上,我需要一個很好的方法來在兩個視圖控制器之間傳遞數據。
非常好的答案。 – 2010-08-08 06:25:45
有趣。但是,看起來父視圖控制器是導航控制器,而不是ItemAddViewController(並且導航控制器的父級是標籤欄控制器) – DVG 2010-08-08 18:11:15