我想从数组EKEvent中获得一个iOS。但我有一个问题。如果我将代码行放在viewDidLoad方法中,它就能工作。
但是,如果我在一个表视图( (-(IBAction)button:(UiButton *)sender) )的按钮中做了这个操作,它就会得到一个EKEvent,但是没有注意到这个事件。
这是我的代码:
EKEvent *event =[eventArray objectAtIndex:0]
NSString *string = event.notes这是完整的代码:
- (void)viewDidLoad {
selectedDate = [NSDate date];
[super viewDidLoad];
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted == NO) {
UIViewController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"needAccess"];
[self.view addSubview:controller.view];
[self addChildViewController:controller];
}
}];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *start = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:selectedDate options:0];
NSDate *end = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[selectedDate dateByAddingTimeInterval:60*60*24] options:0];
NSPredicate *predicate = [store predicateForEventsWithStartDate:start endDate:end calendars:nil];
eventArray = [store eventsMatchingPredicate:predicate];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [eventArray count];
}
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier: @"EventCell"];
EKEvent *event = [eventArray objectAtIndex:indexPath.row];
UIButton *headline = (UIButton *)[cell viewWithTag:1];
headline.layer.cornerRadius = headline.frame.size.width / 2;
headline.clipsToBounds = YES;
headline.tag = indexPath.row;
[headline addTarget:self action:@selector(headlineButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)headlineButton:(UIButton*)sender {
EKEvent *event = [eventArray objectAtIndex:0];
NSArray *notes = [event.notes componentsSeparatedByString:@"//"];
NSString *latitude = [notes objectAtIndex:1];
NSString *longitude = [notes objectAtIndex:2];
}发布于 2015-08-31 22:59:46
这可能是因为单元格或单元格上的按钮都没有EKEvent属性。
你的手机是从模型中加载的。事件数据可能在模型中。
如果您的按钮或单元格需要访问事件数据,您必须决定数据如何通过您的应用程序。您还希望事件有一个单一的真实来源,因此无论您的应用程序的状态如何,您总是可以确定事件的详细信息,无论是编辑、更新、持久化、删除或插入事件。
这些都是您想要解决的问题,因此您非常了解如何存储这些细节,以及您的单元格按钮应该如何访问事件的备注。
https://stackoverflow.com/questions/32320971
复制相似问题