Based on Ramin's reply, you can try this way, which is used by Apple in several samples:
MyListViewController.h
#import "MyAddViewController.h"
@interface MyListViewController : UITableViewController <MyAddViewControllerDelegate> {
}
- (IBAction)add:(id)sender;
@end
MyListViewController.m
// Action for bring up add view controller
- (IBAction)add:(id)sender {
MyAddViewController *addViewController = [[MyAddViewController alloc] initWithStyle:UITableViewStyleGrouped];
addViewController.delegate = self;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addViewController];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[addViewController release];
}
// MyAddViewController's delegate method, dismiss the add view controller in here
- (void)addViewController:(MyAddViewController *)addViewController didAddData:(MyData *)data{
if (data) {
MyDetailViewController *detailViewController = [[MyDetailViewController alloc] initWithStyle:UITableViewStylePlain];
detailViewController.data = data;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
[self dismissModalViewControllerAnimated:YES];
}
MyAddViewController.h
@protocol MyAddViewControllerDelegate;
@class MyData;
@interface MCCourseAddTableViewController : UITableViewController {
@private
MyData *data;
id <MCCourseAddDelegate> delegate;
}
// MyData could be NSManagedObject if you want to use Core Data
@property(nonatomic, retain) MyData *data;
@property(nonatomic, assign) id <MyAddViewControllerDelegate> delegate;
- (void)save;
- (void)cancel;
@end
@protocol MyAddViewControllerDelegate <NSObject>
@optional
- (void)addViewController:(MyAddViewController *)addViewController didAddData:(MyData *)data;;
@end
MyAddViewController.m
- (void)save {
if (self.delegate != nil) {
if ([self.delegate conformsToProtocol:@protocol(MyAddViewControllerDelegate)]){
if ([self.delegate respondsToSelector:@selector(addViewController:didAddData:)]){
// Send data back to List View, to bring up detail view controller and dismiss add view controller
[self.delegate addViewController:self didAddData:self.data];
}
}
}
}
- (void)cancel {
if (self.delegate != nil) {
if ([self.delegate conformsToProtocol:@protocol(MyAddViewControllerDelegate)]){
if ([self.delegate respondsToSelector:@selector(addViewController:didAddData:)]){
// Send nil back to ListView, to dismiss the add view controller only
[self.delegate addViewController:self didAddData:nil];
}
}
}
}