OStack程序员社区-中国程序员成长平台

标题: ios - 使用 MBCalendarKit 创建事件的问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:44
标题: ios - 使用 MBCalendarKit 创建事件的问题

我在我的应用程序中使用 MBCalendarKit,我可以创建这样的事件:

CKCalendarEvent* aCKCalendarEvent = [[CKCalendarEvent alloc] init];
    [_infoDict setObject"Joint Meetings with KY State Horticulture Society, KY Vegetable Growers Association, KY Vineyard Society, and KY Farmer Market Association \nEmbassy Suites\nLexington, KY" forKey"Jan Joint"];
    aCKCalendarEvent.date = [dateformatter dateFromString: @"01/06/2014"];
    aCKCalendarEvent.title = @"Joint Meetings";
    aCKCalendarEvent.info = [_infoDict objectForKey"Jan Joint"];
    [eventsArray addObject: aCKCalendarEvent];
    [_eventsDict setObject: eventsArray forKey: [NSDate dateWithDay:06 month:01 year:2014]];

我可以像这样在同一天创建另一个事件:

[_infoDict setObject"Washington, D.C." forKey"Congress Convenes"];
    aCKCalendarEvent = [[CKCalendarEvent alloc] init];
    aCKCalendarEvent.date = [dateformatter dateFromString: @"01/06/2014"];
    aCKCalendarEvent.title = @"113th Congress Convenes";
    [eventsArray addObject: aCKCalendarEvent];
    [_eventsDict setObject: eventsArray forKey: [NSDate dateWithDay:06 month:01 year:2014]];

然后我可以像这样在不同的日子创建另一个事件:

eventsArray = [[NSMutableArray alloc] init];
    [_infoDict setObject"Hilton Hotel \nMemphis, TN" forKey"Forage & Grassland"];
    aCKCalendarEvent = [[CKCalendarEvent alloc] init];
    aCKCalendarEvent.date = [dateformatter dateFromString: @"01/12/2014"];
    aCKCalendarEvent.title = @"American Forage & Grassland Council Annual Conference";
    [eventsArray addObject: aCKCalendarEvent];
    [_eventsDict setObject: eventsArray forKey: [NSDate dateWithDay:12 month:01 year:2014]];

我正在尝试遍历包含所有事件的 plist 文件以创建所有事件。我可以这样做:

NSString *path = [[NSBundle mainBundle] pathForResource"Events" ofType"plist"];
    NSDictionary *dictPri = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
    NSMutableArray *arrEvents = [[NSMutableArray alloc] initWithArray:[dictPri objectForKey"List"]];

    // Loop through each item in List array of Events.plist

    for (NSDictionary *dict in arrEvents)
    {
        NSString *titleString = nil;
        NSString *date = nil;
        titleString = [NSString stringWithFormat:@"%@",[dict objectForKey:@"Title"]];
        date = [NSString stringWithFormat:@"%@",[dict objectForKey:@"Date"]];
        // NSDate *dateVariable = [dateformatter dateFromString:date];

        // Create events

        eventsArray = [[NSMutableArray alloc] init];
        aCKCalendarEvent = [[CKCalendarEvent alloc] init];
        aCKCalendarEvent.title = titleString;
        aCKCalendarEvent.date = [dateformatter dateFromString: date];
        [eventsArray addObject: aCKCalendarEvent];
        [_eventsDict setObject:eventsArray forKey:aCKCalendarEvent.date];
    }

问题是,如果我在同一天有多个事件,则只会显示最新的事件。我可以取出 eventsArray = [[NSMutableArray alloc] init]; 行,但是新事件不仅会在正确的日子与其他事件一起出现,而且还会在其他日子出现。所以我想我可以输入这样的东西来检查我是否需要该行,然后将事件添加到 eventsDict 但我得到 -[CKCalendarEvent isEqualToDate:]: unrecognized selector sent to instance 0xc6bb880 on if ([[eventsArray objectAtIndex:i]isEqualToDate:aCKCalendarEvent.date]):

BOOL dateExists;

        for (int i = 0; i < [eventsArray count]; i++)
        {
            if ([[eventsArray objectAtIndex:i]isEqualToDate:aCKCalendarEvent.date])
            {
                // Array already contains object with this date

                dateExists = YES;
            }
            else
            {
                dateExists = NO;
            }

            NSLog(@"%c", dateExists);
        }

        if (dateExists == YES)
        {
            [eventsArray addObject:aCKCalendarEvent];
            [_eventsDict setObject:eventsArray forKey:aCKCalendarEvent.date];
        }
        else
        {
            eventsArray = [[NSMutableArray alloc]init];
            [eventsArray addObject:aCKCalendarEvent];
            [_eventsDict setObject:eventsArray forKey:aCKCalendarEvent.date];
        }

所以基本上,我需要一种方法来遍历 plist 并确定程序是否需要在每个新事件上初始化 eventsArray。



Best Answer-推荐答案


您正在将事件与日期进行比较。请参阅下面比较事件日期的代码。

if ([[(CKCalendarEvent *)[eventsArray objectAtIndex:i] date]isEqualToDate:aCKCalendarEvent.date])

关于ios - 使用 MBCalendarKit 创建事件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20634933/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4