1

我需要使用预定义的“Office”位置字符串从所有日历中检索所有 iOS 事件,然后将结果写入 NSLog 和 UITextView。

到目前为止,这是我的代码:

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    @synthesize eventStore = _eventStore;
    @synthesize events     = _events;
    @synthesize eventTextView;

    - (void)viewDidLoad
    {

        // Store a reference to the event store
        EKEventStore *eventStore = [[EKEventStore alloc] init];

       [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted,            
    NSError *error) {
                // handle access here
            if (granted)
                NSLog (@"Access granted");
        }];
        self.eventStore = eventStore;

        // Define a range of event dates we want to display

        NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:(-1*60*60)]; // 1 hour in  

    the past
        NSDate *endDate   = [NSDate dateWithTimeIntervalSinceNow:(60*60*24*365)]; // 1 year  

    from now

        // Create a predicate to search all celndars with our date range

        NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
        endDate:endDate calendars:nil];


        // Query the event store using the predicate.

        NSArray *results       = [self.eventStore eventsMatchingPredicate:predicate];

        //Convert the results to a mutable array and store so we can implement swipe to 

    delete

        NSMutableArray *events = [[NSMutableArray alloc] initWithArray:results];
        self.events = events;


        // Load the events from the event store
        //[self reloadEvents:nil];

        [super viewDidLoad];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (void) reloadEvents:(id)sender
    {
   }

    - (IBAction)getEvents:(id)sender {

        int i;
        int eventCount;

        eventCount = [self.events count];

        for (i = 0; i < eventCount; i++){

            self.eventTextView.text = [[self.events objectAtIndex: i]title], [[self.events 

    objectAtIndex: i]startDate], [[self.events objectAtIndex: i]endDate], [[self.events     

    objectAtIndex: i]location];


            NSLog (@"Event Title: %@, Event Start Date: %@, Event End Date: %@, Event 

    Location: %@",[[self.events objectAtIndex: i]title],
                [[self.events objectAtIndex: i]startDate],
                [[self.events objectAtIndex: i]endDate],
                [[self.events objectAtIndex: i]location]);
        }

    }
    @end

UITextView“eventTextView”中的结果是,我只得到了我的最后一个事件标题,没有别的,但在我的控制台中,我得到了我所要求的一切。我会以正确的方式解决这个问题吗?

另外,我收集到您可以订阅事件存储以获取日历的通知/更改?在上述情况下,这将如何工作?我假设这将取代按钮操作,特别是如果应用程序设置为进入后台?

4

2 回答 2

1

问题在这里:

self.eventTextView.text = [[self.events objectAtIndex: i]title], [[self.events 
objectAtIndex: i]startDate], [[self.events objectAtIndex: i]endDate], [[self.events     
objectAtIndex: i]location];

请注意,您在循环的每次迭代中都替换了 UITextView 的内容。你想要的是连接,在 Objective-C 中最好通过创建一个NSMutableString并调用appendString它的方法来处理。

循环外:

NSMutableString *textVal = [[NSMutableString alloc] init];

在你的循环内:

[textVal appendString:[[self.events objectAtIndex: i]title], [[self.events 
objectAtIndex: i]startDate], [[self.events objectAtIndex: i]endDate], [[self.events     
objectAtIndex: i]location]];

循环后:

self.eventTextView.text = [NSString stringWithString:textVal];
于 2013-01-21T17:09:57.063 回答
1

我怀疑你在 for 循环的每个循环中都覆盖了 UITextfield 中的文本,因此你只得到最后一个。

此外,只有事件标题显示为字符串,其余的我怀疑不是字符串(是 NSDates 等的混合),需要转换为字符串以在 UITextfield 中显示。

于 2013-01-21T17:11:03.837 回答