views:

101

answers:

1

Hi All,

I am using code from Ed Marty's answer for the question here but am having real trouble with a few bits.

On the click of a button I have got the datepicker appearing, but the 'done' button however isn't. I am also getting an error from the line:

[delegate datePickerController:controller didPickDate:datePicker.date];

Error message:

'controller' undeclared (first use in this function)

All in all I have 6 files:

  • ModalDatePickerViewController.m
  • ModalDatePickerViewController.h
  • ModalDatePickerAppDelegate.m
  • ModalDatePickerAppDelegate.h
  • DatePickerController.m
  • DatePickerController.h

My DatePickerController.h is looking like:

    #import <UIKit/UIKit.h>


@class DatePickerController;
@protocol DatePickerControllerDelegate
- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date;
@end

@interface DatePickerController : UIViewController {
    UIDatePicker *datePicker;
    NSObject <DatePickerControllerDelegate> *delegate;
}

@property (nonatomic, retain) UIDatePicker *datePicker;
@property (nonatomic, assign) NSObject <DatePickerControllerDelegate> *delegate;
@end

and the DatePickerController.m:

#import "DatePickerController.h"

@implementation DatePickerController

@synthesize datePicker;
@synthesize delegate;

- (void) loadView {
    self.view = [[[UIView alloc] init] autorelease];
    self.datePicker = [[[UIDatePicker alloc] init] autorelease];
    [self.view addSubview:self.datePicker];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Done" forState:UIControlStateNormal];
    button.center = CGPointMake(160,230);
    [button addTarget:self action:@selector(done) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:button];
}

- (void) done {
    [delegate datePickerController:controller didPickDate:datePicker.date];
}

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

@end

On the main view I have a button that call this class as below:

#import "ModalDatePickerViewController.h"

@implementation ModalDatePickerViewController

- (void) pickDate {
    DatePickerController *screen = [[[DatePickerController alloc] init] autorelease];
    screen.delegate = self;
    [self presentModalViewController:screen animated:YES];
}

- (void) datePickerController:(DatePickerController *)controller didPickDate:(NSDate *)date {
    //[self doSomethingWithDate:date];
    [controller dismissModalViewControllerAnimated:YES];
}
- (IBAction)HitMe:(id)sender {
    [self pickDate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
}

- (void)dealloc {
    [super dealloc];
}
@end

and:

#import <UIKit/UIKit.h>
#import "DatePickerController.h"

@interface ModalDatePickerViewController : UIViewController <DatePickerControllerDelegate> {

}

- (IBAction)HitMe:(id)sender;

@end
+1  A: 

On this line:

[delegate datePickerController:controller didPickDate:datePicker.date];

try replacing controller with self.

drewh