这是我的主视图控制器。我正在使用xml文件中的事件填充自定义日历。这工作正常......我也在添加自己的活动。我为两者使用相同的数组。当我向数组添加新事件时,似乎。它删除了前一个。
#import "CalendarViewController.h"
#import "HeaderCollectionReusableView.h"
#import "CalendarCollectionViewCell.h"
#import "EventViewController.h"
#import "AppDelegate.h"
#import "events.h"
#import "AddViewController.h"
@interface CalendarViewController ()
@end
@implementation CalendarViewController
UIImageView *navBarHairlineImageView;
BOOL _viewDidLayoutSubviewsForTheFirstTime = YES;
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"VIEW DID LOAD");
// Do any additional setup after loading the view.
self.title = @"Calendar";
//\n in xml is encoded as
_viewDidLayoutSubviewsForTheFirstTime = YES;
if (!_calendar) {
[self setCalendar:[NSCalendar currentCalendar]];
}
_date = [[NSDate alloc] init];
NSDateComponents *components = [_calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:_date];
// set calendar from 2000 to current year + 10 years range
long year = components.year;
long month = components.month;
long day = components.day;
// NSLog(@"%ld %ld %ld", day, month, year);
_initialSection = (year - 2000) * 12 + month - 1; // index sections start at 0
components.day = 1;
components.month = 1;
components.year = 2000;
_firstDate = [_calendar dateFromComponents:components];
components.year = year + 10;
components.day = -1;
_lastDate = [_calendar dateFromComponents:components];
UINavigationBar *navigationBar = self.navigationController.navigationBar;
navBarHairlineImageView = [self findHairlineImageViewUnder:navigationBar];
UIView *bottomBorder = [[UIView alloc] init];
bottomBorder.backgroundColor = [UIColor lightGrayColor];
bottomBorder.frame = CGRectMake(0, _days.frame.size.height - 0.5, _days.frame.size.width, 0.5);
[_days addSubview:bottomBorder];
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSData *data = appDelegate.data;
NSString *a = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *b = [a stringByReplacingOccurrencesOfString:@" " withString:@"\n"];
data = [b dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(@"%@", b);
_eventParser = [[NSXMLParser alloc] initWithData:data];
_eventsA = [[NSMutableArray alloc] init];
[_eventParser setDelegate:self];
[_eventParser parse];
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = dirPaths[0];
_databasesPath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"ev.db"]];
// NSFileManager *filemgr = [NSFileManager defaultManager];
const char *dbpaths = [_databasesPath UTF8String];
sqlite3_stmt *statement;
if(sqlite3_open(dbpaths, &_myDB) == SQLITE_OK){
NSString *querySQL = @"SELECT * FROM event";
const char *query_stmt = [querySQL UTF8String];
NSLog(@"I SEE YOUFIRST");
if(sqlite3_prepare_v2(_myDB, query_stmt, -1, &statement, NULL) == SQLITE_OK){
// const char *query_stmt = [querySQL UTF8String];
//query_stmt = [querySQL UTF8String];
// NSMutableArray *allMessages = [[NSMutableArray alloc] init];
NSLog(@"I SEE YOU");
if(sqlite3_prepare_v2(_myDB, query_stmt, -1, &statement, NULL) == SQLITE_OK){
while(sqlite3_step(statement) == SQLITE_ROW){
NSString *typeField = [[NSString alloc] initWithUTF8String:(const char * ) sqlite3_column_text(statement, 0)];
NSString *descriptionField = [[NSString alloc] initWithUTF8String:(const char * ) sqlite3_column_text(statement, 1)];
NSString *dateField = [[NSString alloc] initWithUTF8String:(const char * ) sqlite3_column_text(statement, 2)];
// NSLog(@"%@", dateField);
// NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: typeField, @"typeField", descriptionField, @"descriptionField", dateField, @"dateField", nil];
// [allMessages addObject:dict];
_currentevent.eventType = typeField; //[dict objectForKey:@"typeField"];
_currentevent.eventDate = dateField; //[dict objectForKey:@"dateField"];
_currentevent.eventDescription = descriptionField; //[dict objectForKey:@"descriptionField"];
if (![_eventsA containsObject:_currentevent]){
[self.eventsA addObject:_currentevent];
}
// NSLog(@"Type: %@ Description:%@ Date:%@" , typeField, descriptionField, dateField );
//[_eventsA addObject:dateField];
// [_eventsA addObject:nameField];
//[_eventsA addObject:typeField];
}
sqlite3_finalize(statement);
}
sqlite3_close(_myDB);
}
}
else {
NSLog (@"Failed to add event");
}
NSLog(@"%lu" , (unsigned long)[_eventsA count]);
for(int i = 0; i < [_eventsA count]; i++) {
events *ex = [_eventsA objectAtIndex:i];
NSString *ds = ex.eventDate;
NSString *dd = ex.eventDescription;
NSLog(@"%@ %@", ds, dd);
}
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// Only scroll when the view is rendered for the first time
if (_viewDidLayoutSubviewsForTheFirstTime) {
_viewDidLayoutSubviewsForTheFirstTime = NO;
UICollectionViewLayoutAttributes *attributes = [_calendarCollectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:_initialSection]];
CGRect rect = attributes.frame;
[_calendarCollectionView setContentOffset:CGPointMake(_calendarCollectionView.frame.origin.x, rect.origin.y - 32) animated:NO];
}
}
// find and remove hairline image under top bar
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
-(void)viewWillAppear:(BOOL)animated {
navBarHairlineImageView.hidden = YES;
[self viewDidLoad];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
navBarHairlineImageView.hidden = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//#pragma mark -
#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return [_calendar components:NSCalendarUnitMonth fromDate:_firstDate toDate:_lastDate options:0].month + 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
NSDate *firstOfMonth = [self firstOfMonthForSection:section];
NSRange rangeOfWeeks = [_calendar rangeOfUnit:NSCalendarUnitWeekOfMonth inUnit:NSCalendarUnitMonth forDate:firstOfMonth];
//We need the number of calendar weeks for the full months (it will maybe include previous month and next months cells)
int daysPerWeek = 7;
return (rangeOfWeeks.length * daysPerWeek);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CalendarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"calendarCell" forIndexPath:indexPath];
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSDate *cellDate = [self dateForCellAtIndexPath:indexPath];
NSDateComponents *cellDateComponents = [_calendar components:NSCalendarUnitDay|NSCalendarUnitMonth|NSCalendarUnitYear fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [_calendar components:NSCalendarUnitMonth fromDate:firstOfMonth];
NSDateComponents *todayComponents = [_calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
if(cellDateComponents.month == firstOfMonthsComponents.month) {
NSString *day = @"";
NSDateFormatter *dateFormatter;
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"d";
day = [dateFormatter stringFromDate:cellDate];
cell.dateNumber.text = day;
cell.dateNumber.layer.cornerRadius = 7.0;
cell.dateNumber.clipsToBounds = YES;
if(cellDateComponents.day == todayComponents.day &&
cellDateComponents.month == todayComponents.month &&
cellDateComponents.year == todayComponents.year) {
cell.dateNumber.backgroundColor = [UIColor redColor];
cell.dateNumber.textColor = [UIColor whiteColor];
}
else {
cell.dateNumber.backgroundColor = [UIColor clearColor];
cell.dateNumber.textColor = [UIColor blackColor];
if(indexPath.row % 7 == 0 || (indexPath.row + 1) % 7 == 0)
cell.dateNumber.textColor = [UIColor lightGrayColor];
else
cell.dateNumber.textColor = [UIColor blackColor];
}
cell.event.layer.cornerRadius = 3.0;
cell.event.clipsToBounds = YES;
NSDateFormatter *eventFormatter = [[NSDateFormatter alloc] init];
eventFormatter.dateFormat = @"yyyy-MM-dd";
NSString *dateCell = [eventFormatter stringFromDate:cellDate];
NSString *markedEvent = @"";
for(int i = 0; i < [_eventsA count]; i++) {
events *temp = [_eventsA objectAtIndex:i];
markedEvent = temp.eventDate;
if([markedEvent isEqualToString:dateCell]) {
cell.event.backgroundColor = [UIColor lightGrayColor];
cell.hasEvent = YES;
}
}
}
else {
cell.dateNumber.text = @"";
}
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
#pragma mark - UICollectionViewDelegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSDate *cellDate = [self dateForCellAtIndexPath:indexPath];
//We don't want to select Dates that are "disabled"
if (![self isEnabledDate:cellDate]) {
return NO;
}
NSDateComponents *cellDateComponents = [_calendar components:NSCalendarUnitDay|NSCalendarUnitMonth fromDate:cellDate];
NSDateComponents *firstOfMonthsComponents = [_calendar components:NSCalendarUnitMonth fromDate:firstOfMonth];
return (cellDateComponents.month == firstOfMonthsComponents.month);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
CalendarCollectionViewCell *cell = (CalendarCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.dateNumber.layer.cornerRadius = 7.0;
cell.dateNumber.clipsToBounds = YES;
cell.dateNumber.backgroundColor = [UIColor grayColor];
cell.dateNumber.textColor = [UIColor whiteColor];
_selectedDate = [self dateForCellAtIndexPath:indexPath];
if(cell.hasEvent) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"EEEE MMMM d, yyyy";
NSDate *cellDate = [self dateForCellAtIndexPath:indexPath];
NSString *stringDate = [formatter stringFromDate:cellDate];
NSString *stringType;
NSString *stringDescription;
NSString *tempDate;
NSDateFormatter *fm = [[NSDateFormatter alloc] init];
fm.dateFormat = @"yyyy-MM-dd";
NSString *cd = [fm stringFromDate:cellDate];
for(int i = 0; i <[_eventsA count]; i++){
events *temp1 = [_eventsA objectAtIndex:i];
tempDate = temp1.eventDate;
//NSLog(@"%@", tempDate);
// NSLog(@"%@", stringDate);
// NSLog(@"%@", cd);
if([cd isEqualToString:tempDate]){
//NSLog(@"%@", tempDate);
stringType= temp1.eventType;
stringDescription = temp1.eventDescription;
}
else{
}
}
EventViewController *events = [self.storyboard instantiateViewControllerWithIdentifier:@"eventController"];
events.stringDate = stringDate;
events.stringType = stringType;
events.stringDescription = stringDescription;
[self.navigationController pushViewController:events animated:YES];
}
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
CalendarCollectionViewCell *cell =(CalendarCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
NSDate *cellDate = [self dateForCellAtIndexPath:indexPath];
if([self isTodayDate:cellDate]) {
cell.dateNumber.backgroundColor = [UIColor redColor];
cell.dateNumber.textColor = [UIColor whiteColor];
}
else {
cell.dateNumber.backgroundColor = [UIColor clearColor];
cell.dateNumber.textColor = [UIColor blackColor];
}
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
HeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"calendarHeader" forIndexPath:indexPath];
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:12];
headerView.month.font = font;
headerView.month.textColor = [UIColor redColor];
UIView *bottomBorder = [UIView new];
bottomBorder.backgroundColor = [UIColor lightGrayColor];
bottomBorder.frame = CGRectMake(0, headerView.frame.size.height - 1, headerView.frame.size.width, 1);
[headerView addSubview:bottomBorder];
NSDateFormatter *headerDateFormatter = [[NSDateFormatter alloc] init];
headerDateFormatter.calendar = _calendar;
headerDateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"yyyy LLLL" options:0 locale:_calendar.locale];
NSString *headerTitle = [headerDateFormatter stringFromDate:[self firstOfMonthForSection:indexPath.section]].uppercaseString;
headerView.month.text = headerTitle;
reusableview = headerView;
}
return reusableview;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat width = self.view.frame.size.width / 7.0;
CGFloat height = width;
return CGSizeMake(width, height);
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
UICollectionViewFlowLayout *flowLayout = (id)self.calendarCollectionView.collectionViewLayout;
[flowLayout invalidateLayout]; //force the elements to get laid out again with the new size
}
// Calendar methods
- (NSDate *)firstOfMonthForSection:(NSInteger)section
{
NSDateComponents *offset = [NSDateComponents new];
offset.month = section;
return [_calendar dateByAddingComponents:offset toDate:_firstDate options:0];
}
- (NSDate *)dateForCellAtIndexPath:(NSIndexPath *)indexPath
{
NSDate *firstOfMonth = [self firstOfMonthForSection:indexPath.section];
NSInteger ordinalityOfFirstDay = [_calendar ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitWeekOfMonth forDate:firstOfMonth];
NSDateComponents *dateComponents = [NSDateComponents new];
dateComponents.day = (1 - ordinalityOfFirstDay) + indexPath.item;
return [_calendar dateByAddingComponents:dateComponents toDate:firstOfMonth options:0];
}
- (BOOL)isEnabledDate:(NSDate *)date
{
NSDate *clampedDate = [self clampDate:date toComponents:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay)];
if (([clampedDate compare:_firstDate] == NSOrderedAscending) || ([clampedDate compare:_lastDate] == NSOrderedDescending)) {
return NO;
}
return YES;
}
- (NSDate *)clampDate:(NSDate *)date toComponents:(NSUInteger)unitFlags
{
NSDateComponents *components = [_calendar components:unitFlags fromDate:date];
return [_calendar dateFromComponents:components];
}
- (BOOL)isTodayDate:(NSDate *)date
{
return [self clampAndCompareDate:date withReferenceDate:[NSDate date]];
}
- (BOOL)isSelectedDate:(NSDate *)date
{
if (!_selectedDate) {
return NO;
}
return [self clampAndCompareDate:date withReferenceDate:_selectedDate];
}
- (BOOL)clampAndCompareDate:(NSDate *)date withReferenceDate:(NSDate *)referenceDate
{
NSDate *refDate = [self clampDate:referenceDate toComponents:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay)];
NSDate *clampedDate = [self clampDate:date toComponents:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay)];
return [refDate isEqualToDate:clampedDate];
}
#pragma mark - NSXMLParser Delegate
- (void) parserDidStartDocument:(NSXMLParser *)parser {
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"event"])
_currentevent = [[events alloc] init];
}
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
_currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"type"]) {
_currentevent.eventType = _currentNodeContent;
}
if([elementName isEqualToString:@"date"]) {
_currentevent.eventDate = _currentNodeContent;
}
if([elementName isEqualToString:@"description"]) {
_currentevent.eventDescription = _currentNodeContent;
}
if([elementName isEqualToString:@"event"]) {
[_eventsA addObject:_currentevent];
}
}
- (void) parserDidEndDocument:(NSXMLParser *)parser {
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:@"segueAdd"]) {
AddViewController *modalVC = (AddViewController *)segue.destinationViewController;
// modalVC.cVC = self;
modalVC.sDate = _selectedDate;
}
}
- (IBAction)todayButton:(id)sender {
UICollectionViewLayoutAttributes *attributes = [_calendarCollectionView layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:_initialSection]];
CGRect rect = attributes.frame;
[_calendarCollectionView setContentOffset:CGPointMake(_calendarCollectionView.frame.origin.x, rect.origin.y - 32) animated:YES];
}
- (IBAction)addButton:(id)sender {
[self performSegueWithIdentifier:@"segueAdd" sender:self];
}
@end
这是我的视图控制器,我将事件添加到sqlite
#import "AddViewController.h"
#import "CalendarViewController.h"
@interface AddViewController ()
@end
@implementation AddViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"This is the Date:%@", _sDate);
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = dirPaths[0];
// Build the path to the database file
_databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:@"ev.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: _databasePath ] == NO) {
const char *dbpath = [_databasePath UTF8String];
_status.text = @"No TABLE";
if (sqlite3_open(dbpath, &_evDB) == SQLITE_OK) {
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS EVENT (TYPE TEXT, DESCRIPTION TEXT, DATE VARCHAR(255))";
if (sqlite3_exec(_evDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK) {
_status.text = @"Failed to create table";
}
sqlite3_close(_evDB);
} else {
_status.text = @"Failed to open/create database";
}
}
// Do any additional setup after loading the view.
// [CalendarViewController.view.eventsA addObject:_currentevent];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
UITouch *touch = [[event allTouches] anyObject];
if ([_etype isFirstResponder] && (_etype != touch.view)) {
// _inputText lost focus - close keyboard
[_etype resignFirstResponder];
}
if ([_edescription isFirstResponder] && (_edescription != touch.view)) {
// _inputText lost focus - close keyboard
[_edescription resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:@"goBack"]) {
CalendarViewController *destViewController = (CalendarViewController *)segue.destinationViewController;
destViewController.myDB = _evDB;
destViewController.databasesPath = _databasePath;
destViewController.eventDate = _cd;
destViewController.eventType = _type1;
destViewController.eventDescription = _d1;
destViewController.eventsA = _mArray;
}
}
- (IBAction)addEvent:(id)sender {
// NSMutableArray *temporaryArray = [NSMutableArray arrayWithObject:_sDate];
//= [NSMutableArray arrayWithObject:selectedRow];
// NSLog(@"%@", _sDate);
//NSLog(@"%@", type);
//NSLog(@"%@", description);
//_cVC.eventsA = temporaryArray;
_type1 = [[NSString alloc]initWithString:[_etype text]];
_d1 = [[NSString alloc]initWithString:[_edescription text]];
// NSLog(@"%@", type1);
//NSLog(@"%@", description);
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
NSDateFormatter *fm = [[NSDateFormatter alloc] init];
fm.dateFormat = @"yyyy-MM-dd";
_cd = [fm stringFromDate:_sDate];
NSLog(@"%@" , _cd);
// save
if (sqlite3_open(dbpath, &_evDB) == SQLITE_OK) {
NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO EVENT VALUES (\"%@\", \"%@\", \"%@\")", _type1, _d1, _cd];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_evDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE) {
_status.text = @"Event added";
_etype.text = @"";
_edescription.text = @"";
// UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
/*CalendarViewController *destViewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
destViewController.myDB = _myDB;
destViewController.databasesPath = _databasePath;
destViewController.eventDate = cd;
destViewController.eventType = type1;
destViewController.eventDescription = description;
*/
// super.navigationController.viewDidLoad;
[self.navigationController popToRootViewControllerAnimated:YES];
//[self performSegueWithIdentifier:@"goBack" sender:self];
// CalendarViewController *cal = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
//[self.navigationController pushViewController:cal animated:YES];
// _cevent.eventType =_type1;
//_cevent.eventDate = _cd;
//_cevent.eventDescription = _d1;
//[_mArray addObject:_cevent];
} else {
_status.text = @"Failed to add event";
}
sqlite3_finalize(statement);
sqlite3_close(_evDB);
}
}
@end
答案 0 :(得分:0)
if (![_eventsA containsObject:_currentevent]) {
[self.eventsA addObject:_currentevent];
Problem 1. After one [self.eventsA addObject:_currentevent]
, [_eventsA containsObject:_currentevent]
will return YES
.
Problem 2. NSMutableArray
stores pointers to objects and doesn't copy an object when it is added. If you add _currentevent
to self.eventsA
and then change the properties of _currentevent
, the properties of the added _currentevent
will change. If you remove if (![_eventsA containsObject:_currentevent])
and keep adding the same object, which is possible, all elements of _eventsA
will have the properties of the last _currentevent
.
Solution: create a new local event object in each iteration of the while loop.