gpt4 book ai didi

iphone - KAL Calendar,如何实现 KalDataSource 以便我可以显示 iOS 内置日历应用程序中的事件?

转载 作者:行者123 更新时间:2023-11-29 04:09:01 24 4
gpt4 key购买 nike

我已成功将 Kal Calendar 集成到我的应用程序中,这是我显示日历的方法

-(void)showDepartDatePicker{
NSLog(@"showDepartDatePicker");
if(_departDatePicker != nil){
[self.navigationController pushViewController:_departDatePicker animated:YES];
}else{
_departDatePicker = [[KalViewController alloc] init];
_departDatePicker.title = @"Departure Date";
_departDatePicker.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Today" style:UIBarButtonItemStyleBordered target:self action:@selector(showAndSelectTodayDeparturePicker)];
_departDatePicker.kvcDelegate = self;
[self.navigationController pushViewController:_departDatePicker animated:YES];
}

}

我在 KalViewController.h 中添加了以下内容,

@protocol KalViewControllerDelegate <NSObject>
@required
- (void)didSelectDate:(KalDate *)date andLoaded:(BOOL)loaded;

@end

@property (nonatomic, assign) id <KalViewControllerDelegate> kvcDelegate;

并在我的 viewController 中实现此委托(delegate)方法为

- (void)didSelectDate:(KalDate *)date andLoaded:(BOOL)loaded
{
NSLog(@"Title : %@",[self.navigationController.visibleViewController title]);
[self.navigationController popViewControllerAnimated:YES];
}

现在,根据我的问题,我想实现 KalDataSource,以便它显示标有事件的日期,并选择它在月 View 下方可用的表格 View 中显示事件详细信息。
如果您是 Kal Calendar 的新手,请参阅此链接 https://github.com/klazuka/Kal

第二个问题,这是我如何从 KalViewController.m 调用委托(delegate)方法

- (void)didSelectDate:(KalDate *)date
{
self.selectedDate = [date NSDate];
NSDate *from = [[date NSDate] cc_dateByMovingToBeginningOfDay];
NSDate *to = [[date NSDate] cc_dateByMovingToEndOfDay];
[self clearTable];
[dataSource loadItemsFromDate:from toDate:to];
[tableView reloadData];
[tableView flashScrollIndicators];
//line below calls my delegate method
[self.kvcDelegate didSelectDate:date andLoaded:_loaded];
}

发生的情况是,当我调用 showDepartDatePicker 将 KalViewController 推送到我的导航堆栈时,它调用我的委托(delegate)方法 2 次(应该在日期选择时调用),然后对于每个日期选择再次调用该委托(delegate)方法(1 次) 。

即使我想限制这个日历不显示过去的日期!请帮我解决这个问题。

最佳答案

  1. 定义一个实现KalDataSource协议(protocol)的类。请参阅下面的示例,了解实现 KalDataSource 协议(protocol)的类。

    //header file
    #import Kal.h"
    @interface MyClass : NSObject <KalDataSource>

    @property (nonatomic, weak) id<KalDataSourceCallbacks> kalCallbackDelegate;
    @property (nonatomic, strong) NSArray *events;

    @end

    ----------------------
    //implementation file

    - (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)callbackDelegate
    {
    //If you already have the events between fromDate and toDate then just call
    [callbackDelegate loadedDataSource:self];

    //Else store the callback variable in a property and do an asyncrhonous
    //call to load the events.
    self.kalCallbackDelegate = callbackDelegate;

    //When the Asynchronous call is done, call
    [self.kalCallbackDelgate loadedDataSource:self];

    }

    - (void)removeAllItems
    {
    self.eventsForDay = nil;
    }

    - (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate
    {
    //self.events may have multiple events with the same date. This pulls only the unique dates.
    //Also assumes that the object has an eventDate property for the beginning of the day
    NSMutableSet *uniqueDatesSet = [NSMutableSet setWithArray:[self.events valueForKeyPath:@"@distinctUnionOfObjects.eventDate"]];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self >= %@ && self <= %@", fromDate, toDate];
    NSArray *uniqueDates = [[uniqueDatesSet allObjects] filteredArrayUsingPredicate:predicate];

    return uniqueDates;

    }

    - (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
    {
    //filter for the events that occur between fromDate and toDate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"eventDate >= %@ && eventDate <= %@", fromDate, toDate];
    NSArray *filteredArray = [self.events filteredArrayUsingPredicate:predicate];

    self.eventsForDay = [filteredArray sortedArrayUsingSelector:@selector(compareByEventTime:)];
    }
  2. 要呈现 UITableViewCells,请在 KalDataSource 类中实现 tableView:cellForRowAtIndexPath:,就像在 UITableView 中一样。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    Event *event = [self.events objectAtIndex:indexPath.row];

    static NSString *identifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.textLabel.text = event.title;

    return cell;
    }
  3. 如果您想知道何时选择 UITableViewCell,请定义一个实现 UITableViewDelegate 协议(protocol)的类并设置 _departDatePicker.delegate等于该类。然后您可以在该类中实现常规的 UITableViewDelegate 方法。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
    Event *event = [self.events objectAtIndex:indexPath.row];
    MyViewController *viewController = [[UIStoryboard storyboardWithName:@"iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"eventInfo"];
    viewController.event = event;
    [self.navigationController pushViewController:viewController animated:YES];
    }

关于iphone - KAL Calendar,如何实现 KalDataSource 以便我可以显示 iOS 内置日历应用程序中的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14702829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com