您需要使用委托从视图控制器传回值,无论它们是通过导航控制器推送还是模态显示。就像是:
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
现在在父视图控制器中你做:
- (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
}