這是我的詳細視圖中的代碼它可以刪除事件當你來到詳細視圖按下開關添加事件,然後再按一下刪除它。然而,當u來詳細視圖,然後按返回(使用導航),然後來到Ø在同一個頁面再次,它不能刪除的情況,請幫如何刪除事件包中的事件
有更多的功能,但我刪除了只專注於開關和EVENTKIT
//
// DetailScheduleViewController.m
// Register
//
// Created by junejubu on 3/10/2558 BE.
// Copyright (c) 2558 Thananont Aunsiripant. All rights reserved.
//
- (void)viewDidLoad {
[super viewDidLoad];
//NSLog(@"check show id sch ::%@",self.Show_ID);
self.Switch.on = NO;
self.lblNotification.text = @"Notification OFF";
[self getUserNotification];
[self getUserFollow];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)getUserNotification
{
NSString *urlStr2 = @"http://ptvshowthai.com/getNotification.php";
NSURL *url2 = [NSURL URLWithString:urlStr2];
self.getnoti = [ASIFormDataRequest requestWithURL:url2];
self.getnoti.requestMethod = @"POST"; //DELETE, PUT
self.getnoti.delegate = self;
self.getnoti.timeOutSeconds = 30;
[self.getnoti setPostValue: self.FK_User_ID forKey:@"sMemberID"];
[self.getnoti setValidatesSecureCertificate:NO];
[self.getnoti startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"requestFinished");
if (request == self.getnoti) {
self.dictnoti = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
self.NOTI = [[NSMutableArray alloc] init];
for (NSDictionary *dataDic in self.dictnoti)
{
// unsigned long count2 = [self.dict2 count];
NSString *ShowID2 = [dataDic objectForKey:@"Show_ID"];
self.str2 = [dataDic objectForKey:@"Event_Store"];
// NSLog(@"check str2 :: %@",self.str2);
[self.NOTI addObject:ShowID2];
}
NSLog(@"Show who noti ::%@",self.NOTI);
// NSLog(@"check str2 :: %@",self.str2);
}
if(request ==self.getFollow){
self.dict2 = [NSJSONSerialization JSONObjectWithData:[request responseData] options:NSJSONReadingAllowFragments error:nil];
NSLog(@"Dict getFollow :: %@",self.dict2);
//unsigned long count2 = [self.dict2 count];
//NSLog(@"show count ::%lu",count2);
_Show = [[NSMutableArray alloc] init];
// NSDictionary *dict;
for (NSDictionary *dataDic in self.dict2)
{
// unsigned long count2 = [self.dict2 count];
NSString *ShowID = [dataDic objectForKey:@"Show_ID"];
[self.Show addObject:ShowID];
}
if ([self.Show containsObject: self.Show_ID]) {
_lblFollow.text = @"Following";
_lblFollow.textColor = [UIColor greenColor];
_i = @"0";
NSLog(@"check i green :: %@",_i);
self.Switch.hidden = NO;
NSLog(@"check self.noti : %@",self.NOTI);
if ([self.NOTI containsObject: self.Show_ID]){
self.Switch.on = YES;
}
self.lblNotification.hidden = NO;
[self.Switch addTarget:self action:@selector(Onoff) forControlEvents:UIControlEventValueChanged];
}
else{
_lblFollow.text = @"Follow";
_lblFollow.textColor = [UIColor redColor];
_i = @"1";
NSLog(@"check i red :: %@",_i);
self.Switch.hidden = YES;
self.Switch.on = NO;
self.lblNotification.hidden = YES;
[self.Switch addTarget:self action:@selector(Onoff) forControlEvents:UIControlEventValueChanged];
}
}
}
-(void)addevent{
_eventStore = [[EKEventStore alloc] init];
if ([_eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[_eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted){
addEventgranted = 1;
EKEvent *event = [EKEvent eventWithEventStore:_eventStore];
[event setTitle:self.showNameTitle];
[event setStartDate: self.today];
[event setEndDate:[[NSDate alloc]initWithTimeInterval:self.duration sinceDate:event.startDate]];
NSTimeInterval alarmOffset = -300;
EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:alarmOffset];
[event addAlarm:alarm];
[event setCalendar:[_eventStore defaultCalendarForNewEvents]];
NSError *err;
[_eventStore saveEvent:event span:EKSpanThisEvent error:&err];
self.str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];
NSLog(@"check self.str :: %@",self.str);
NSString *url = @"http://ptvshowthai.com/insertNotification.php";
NSURL *urls = [NSURL URLWithString:url];
self.formData = [ASIFormDataRequest requestWithURL:urls];
self.formData.requestMethod = @"POST";
self.formData.delegate = self;
self.formData.timeOutSeconds = 30;
[self.formData setPostValue: self.FK_User_ID forKey:@"sMemberID"];
[self.formData setPostValue: self.Show_ID forKey:@"showID"];
NSLog(@"check str::: %@",self.str);
[self.formData setPostValue: self.str forKey:@"event"];
[self.formData setValidatesSecureCertificate:NO];
[self.formData startAsynchronous];
_lblFollow.text = @"Following";
_lblFollow.textColor = [UIColor greenColor];
NSLog(@"Follow");
[self getUserNotification];
}
}];
}
}
-(void)alert
{
if (addEventgranted == 1) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Event Successfully added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
-(void)Onoff
{
if (self.Switch.on) {
[self addevent];
[self performSelector:@selector(alert) withObject:nil afterDelay:0.3];
self.lblNotification.text [email protected]"Notification On";
}
else
{
NSLog(@"check str22::%@",self.str2);
EKEvent* event2 = [_eventStore eventWithIdentifier:self.str2];
if (event2 != nil) {
NSError* error = nil;
[_eventStore removeEvent:event2 span:EKSpanThisEvent error:&error];
}
self.lblNotification.text [email protected]"Notification OFF";
NSString *url = @"http://ptvshowthai.com/deleteNotification.php";
NSURL *urls = [NSURL URLWithString:url];
self.formData = [ASIFormDataRequest requestWithURL:urls];
self.formData.requestMethod = @"POST";
self.formData.delegate = self;
self.formData.timeOutSeconds = 30;
[self.formData setPostValue: self.FK_User_ID forKey:@"sMemberID"];
[self.formData setPostValue: self.Show_ID forKey:@"showID"];
[self.formData setValidatesSecureCertificate:NO];
[self.formData startAsynchronous];
_lblFollow.text = @"Following";
_lblFollow.textColor = [UIColor greenColor];
NSLog(@"Follow");
[self getUserNotification];
}
}
檢查EventStore是否獲得零或不是。如果是這種情況alloc viewStore at viewWillAppear ..! – Vidhyanand 2015-03-13 12:45:51