views:

63

answers:

1

So I have a tableview as a root screen and an add button that presents a new screen that has a datepicker. When I present the new view, I set the root screen to delegate.

Now, I need to pass the data from the uidatepicker screen back to its delegate, but I can't remember how that was supposed to work...

I know that I should be able to call a method that exists in the root screen from the new datepicker view, but again, I can't remember the syntax.

I know I have done it before, but I its been a while since I have been using Cocoa and I am just trying to recall.

Thanks for your help!!

A: 

This should be enough to refresh your memory.

MyDatePickerViewController.h:

@protocol MyDatePickerViewControllerDelegate;

@interface MyDatePickerViewController;

MyDatePickerViewController : UIViewController
{
    id<MyDatePickerViewControllerDelegate> delegate;

    NSDate* selectedDate;
}

@property (nonatomic, assign) id<MyDatePickerViewControllerDelegate> delegate;
@property (nonatomic, retain) NSDate* selectedDate;

@end

@protocol MyDatePickerViewControllerDelegate
- (void)myDatePickerViewControllerDidFinish:(MyDatePickerViewController*)myDatePickerViewController;
@end

MyDatePickerViewController.m:

@implementation MyDatePickerViewController

@synthesize delegate;
@synthesize selectedDate;


- (void)dealloc
{
    [selectedDate release];
    [super dealloc];
}

- (void)someMethodCalledWhenUserIsDonePickingDate
{
    [delegate myDatePickerViewControllerDidFinish:self];
}

MyRootViewController.h:

#import MyDatePickerViewController

@interface MyRootViewController : UIViewController <MyDatePickerViewControllerDelegate>
{
    ...
}

@end

MyRootViewController.m:

@implementation MyRootViewController

- (void)myDatePickerViewControllerDidFinish:(MyDatePickerViewController*)myDatePickerViewController
{
    NSDate* date = myDatePickerViewController.selectedDate
    // Do something with the date that was chosen
}

@end
Shaggy Frog