How to implement a modal date picker?

丶灬走出姿态 提交于 2019-12-12 13:19:46

问题


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:


On this line:

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

try replacing controller with self.



来源:https://stackoverflow.com/questions/3820706/how-to-implement-a-modal-date-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!