デリゲートを使用して、値がナビゲーション コントローラー経由でプッシュされたか、モーダルに表示されたかに関係なく、ビュー コントローラーから値を返す必要があります。何かのようなもの:
MyEditController.h:
@class MyEditController;
@protocol MyEditControllerDelegate <NSObject>
@optional
- (void)myEditController:(MyEditController *)myEditController
updatedThing:(Thing *)thing;
- (void)myEditController:(MyEditController *)myEditController
deletedThing:(Thing *)thing;
@end
@interface MyEditController : UITableViewController
{
id<MyEditControllerDelegate> _delegate;
Thing *_thing;
...
}
@property (assign, nonatomic, readwrite) id<MyEditControllerDelegate> delegate;
@property (retain, nonatomic, readwrite) Thing *thing;
...
@end
MyEditController.m:
...
- (void)updateThing
{
...
if ([_delegate respondsToSelector:@selector(myEditController:updatedThing:)])
{
[_delegate myEditController:self updatedThing:self.thing];
}
[self.navigationController popViewControllerAnimated:YES];
}
- (void)deleteThing
{
...
if ([_delegate respondsToSelector:@selector(myEditController:deletedThing:)])
{
[_delegate myEditController:self deletedThing:self.thing];
}
[self.navigationController popViewControllerAnimated:YES];
}
...
@end
親View Controllerで次のことを行います。
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MyEditController *myEditController = [[[MyEditController alloc] initWithNibName:@"EditView" bundle:nil] autorelease];
myEditController.delegate = self;
myEditController.thing = [self.thingList objectAtIndex:[indexPath row]];
[self.navigationController pushViewController:myEditController animated:YES];
}
- (void)myEditController:(MyEditController *)myEditController
updatedThing:(Thing *)thing
{
// Update thing in self.thingList
// Reload table view row
}
- (void)myEditController:(MyEditController *)myEditController
deletedThing:(Thing *)thing
{
// Delete thing from self.thingList
// Delete table view row
}