【问题标题】:Find iOS Calendars查找 iOS 日历
【发布时间】:2011-09-04 08:59:52
【问题描述】:

我有一个以编程方式创建日历事件的应用程序,它正在运行,我只是做了它,以便它检索用户日历并将它们显示在选择器中,但我有内存泄漏。你能看到它,因为我尝试释放所有东西......此外,我的主要问题是如何将它保存到用户选择的日历中,[event setCalendar:calendararray];不起作用。 calendararray 是一个 EKCalendar *calendararray,我将用户选择的日历设置为它。为什么这不起作用????我如何使它工作...

日历.m

 #import "calendar.h"


 @implementation calendar
 @synthesize delegate;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];


EKEventStore *eventStore = [[EKEventStore alloc] init];

/* These are the calendar types an iOS Device can have. Please note
 that the "type" property of an object of type EKCalendar
 is of type EKCalendarType. The values in the "CalendarTypes"
 array reflect the exact same values in the EKCalendarType
 enumeration, but as NSString values */
NSArray *calendarTypes = [NSArray arrayWithObjects:
                          @"Local",
                          @"CalDAV",
                          @"Exchange",
                          @"Subscription",
                          @"Birthday",
                          nil];

/* Go through the calendars one by one */
NSUInteger counter = 1;
for (EKCalendar *thisCalendar in eventStore.calendars){

    /* The title of the calendar */
    NSLog(@"Calendar %lu Title = %@", 
          (unsigned long)counter, thisCalendar.title);

【问题讨论】:

    标签: iphone objective-c xcode ipad ios4


    【解决方案1】:

    EKEventStore 有一个calendars 属性,它是一个 EKCalendar 实例的 NSArray。然后,您可以获取每个 EKCalendar 的标题并将它们全部显示给用户,以便他们可以选择他们希望将新事件添加到的日历。

    【讨论】:

    • 好的,这向我展示了如何到达某个地方,我找到了在日志中显示它的代码,但是我环顾四周,找不到如何将其转换为我在上面发布的代码的字符串。感谢所有的帮助。
    【解决方案2】:
    NSLog(@"Calendar %lu Title = %@", (unsigned long)counter, thisCalendar.title);
    NSString *title = [NSString stringWithFormat:@"Calendar %lu Title = %@", (unsigned long)counter, thisCalendar.title];
    NSLog(title);
    

    【讨论】:

    • 但是你所做的每次只给我一个卡路里,所以我不能把它放在桌子上,对吧?
    • 你必须在你的 for 循环中这样做。
    • 这段代码除了记录两次同样的事情之外还有什么作用?
    • 啊,我明白了,回答 OP 对 smountcastle 的回答的评论。没关系!
    【解决方案3】:

    我不确定您发布的所有代码是否来自同一版本,但如果您将它们放在一起,我至少会看到以下问题:

    你说

    [event setCalendar:calendararray];
    

    没用。

    似乎calendararray 是在eventview.mcalendararray: 方法中设置的。

    这又从calendar.mpickerView:didSelectRow: 调用,它从arrayColors 获取选定的对象。

    calendar.mviewDidLoad 方法中,arrayColors 被初始化为用户的日历标题,而不是日历。 所以你最终给EKEventsetCalendar:方法一个NSString而不是EKCalendar

    不知道你程序的其余部分,作为修复我会尝试将EKCalendar 对象本身保留在arrayColors 中。

    所以,在calendar.m 尝试更改

        [arrayColors addObject:thisCalendar.title];
    

    viewDidLoad

        [arrayColors addObject:thisCalendar];
    

    然后将pickerView:titleForRow:改为

    - (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
       EKEvent *event = [arrayColors objectAtIndex:row]
       return event.title;
    }
    

    最后,在eventview.m 更改calendararray:

    - (void) calendararray:(EKCalendar *)array{
    NSLog(@"calendarNameTextFieldStringFromTable %@", array);
    
    calendararray = array;
    calendarLabel.text = array.title;
    calendarLabel.textColor = [UIColor brownColor];
    
    }
    

    【讨论】:

    • 在一个更一般的注释上:* 当你编译这个时你得到任何警告吗?你应该,他们应该帮助你找到问题
    • * 请不要将您的问题编辑为完全不同的问题(即“我如何找到所有用户的日历?”与“为事件设置特定日历对我不起作用");改为打开一个新问题;否则,对于只会看到最新版本问题的新读者来说,人们为第一版问题给出的答案似乎是荒谬的
    • * calendararray: 方法在内存管理方面看起来很可疑;该数组可能应该被保留(看看Memory Management Programming Guide,尤其是Accessor Methods上的部分。
    • * 你的变量名让读者(可能还有你自己)感到困惑(calendararray 不是数组,arrayColors 与颜色无关,等等)
    • 是的,我已经尝试了上面的方法,我得到它可以处理来自各地的位,但你能帮我解决我遇到的错误吗?谢谢stackoverflow.com/questions/6352689/ekcalendar-error
    【解决方案4】:

    试试这个方法:

    #import "EventTestViewController.h"
    
    #import "EventKit/EventKit.h"
    
    @implementation EventTestViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        EKEventStore *eventStore = [[EKEventStore alloc] init];
    
        EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
        event.title     = @"EVENT TITLE";
    
        event.startDate = [[NSDate alloc] init];
        event.endDate   = [[NSDate alloc] initWithTimeInterval:600 sinceDate:event.startDate];
    
        [event setCalendar:[eventStore defaultCalendarForNewEvents]];
        NSError *err;
        [eventStore saveEvent:event span:EKSpanThisEvent error:&err];       
    }
    

    【讨论】:

    • 它保存在“日历”中,而不是我选择的工作
    【解决方案5】:

    好的,我知道你想要什么,这是我的方式:

    EKEventStore *eventStore = [[EKEventStore alloc] init];
    
    NSArray *calendarArray = [eventStore calendars]; //Here you get all the device calendars
    
    EKEvent *event = [EKEvent eventWithEventStore:eventStore];
    event.title = @"Event Title";
    
    event.startDate = [[NSDate alloc] init]; event.endDate = [[NSDate alloc] initWithTimeInterval:816 sinceDate:event.startDate];
    
    [event setCalendar:[calendarArray objectAtIndex:1]]; //here you set which calendar you want.. 
    NSError *err;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
    

    您可以使用下面的代码使用 FOR 循环迭代“索引”的 NSLog,以了解您想要哪个。

     NSLog(@"Cal name> %@. Index of cal> %i", [[calendarArray objectAtIndex:index] title], index);
    

    希望对你有帮助

    【讨论】:

    • 是的,我已经尝试了上面的方法,我得到它可以处理来自各地的位,但你能帮我解决我遇到的错误吗?谢谢stackoverflow.com/questions/6352689/ekcalendar-error
    • 当然,我会去看看!如果你让它工作,请通过选择最合适的答案作为正确的绿色勾号来关闭此问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    相关资源
    最近更新 更多