有关将数据传递给iOS中的委托的问题

时间:2010-09-07 17:20:17

标签: iphone delegates ios uidatepicker

所以我有一个tableview作为根屏幕和一个添加按钮,它提供了一个具有日期选择器的新屏幕。当我呈现新视图时,我将根屏幕设置为委托。

现在,我需要将数据从uidatepicker屏幕传递回其委托,但我不记得那应该如何工作......

我知道我应该能够从新的datepicker视图中调用根屏幕中存在的方法,但同样,我不记得语法。

我知道我以前做过这件事,但是我已经用了一段时间了,因为我一直在使用Cocoa而我只想回忆一下。

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

这应该足以刷新你的记忆。

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