NSDate returns wrong date from database - iphone

I have an application that user can order some items and by confirming the order the date of the order is saved and inserted in a sqlite database.
Then later the user can see the order list and the order date and there's an option that user can delete the orders up to whatever date they choose.
Up to this part there's no problem at all, when I select the orders to be shown the date which was returned by database is correct.
The problem occurs when the user wants to remove orders based of the date they choose in the date picker, at this point the date I'm getting from my order object is always before the correct date, for example:
-Order date is 2011/12/20 14:24:23 +0000
-the order date shown to user is the same as above
-the order date when I want to compare dates together is 2011/12/19 14:24:23 +0000
any ideas?
edit:
#import "OrderListViewController.h"
#implementation OrderListViewController
#synthesize newOrderTabelView, transferedOrderTableView, salesOrderArr;
#synthesize newOrderTableViewController, transferedOrderViewController,salesOrder;
#synthesize newOrderTableViewHeader, transferedOrderTableViewHeader, datePicker,isPickerHidden;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
isPickerHidden=YES;
[self initializeTableData];
}
return self;
}
- (void)dealloc
{
[newOrderTabelView release];
[transferedOrderTableView release];
[newOrderTableViewController release];
[transferedOrderViewController release];
[salesOrderArr release];
[newOrderTableViewController release];
[transferedOrderViewController release];
[salesOrder release];
[datePicker release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-(void)editSaleOrderForEditButton
{
YourCartViewController *yourCartViewController=[[YourCartViewController alloc] initWithNibName:#"YourCartViewController" bundle:nil];
yourCartViewController.title=#"Felton";
[self.navigationController pushViewController:yourCartViewController animated:YES];
[yourCartViewController release];
}
-(IBAction)deleteOrderButton_Tapped
{
if(isPickerHidden)
{
datePicker.hidden=NO;
isPickerHidden=NO;
}
else if(!isPickerHidden)
{
NSDate* pickerDate = [datePicker date];
datePicker.hidden=YES;
isPickerHidden=YES;
for(SalesOrder* order in salesOrderArr)
{
NSDate* orderDate = order.Date;
/*if([orderDate earlierDate:pickerDate])
{
order.isDeleted = YES;
}*/
switch([orderDate compare:pickerDate])
{
case NSOrderedAscending:
order.isDeleted = YES;
break;
case NSOrderedSame:
order.isDeleted = YES;
break;
case NSOrderedDescending:break;
}
KDSDataSalesOrder* tSaleOrder = [[KDSDataSalesOrder alloc] init];
[tSaleOrder updateSaleOrderIsDeleted:order];
[tSaleOrder release];
}
[self reloadNewOrderTable];
}
}
-(void)initializeTableData
{
KDSAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
KDSDataSalesOrder *dataSalesOrder=[[KDSDataSalesOrder alloc] init];
NSArray* tNewSalesOrderArr=[dataSalesOrder selectSalesOrdersforSalesPerson:appDelegate.loggedInSalesPerson IsUploaded:NO IsDeleted:NO];
self.salesOrderArr=tNewSalesOrderArr;
[dataSalesOrder release];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
datePicker.hidden = YES;
[super viewDidLoad];
if(newOrderTableViewController == nil)
{
NewOrderTableViewController *tNewOrderTableViewController = [[NewOrderTableViewController alloc] init];
self.newOrderTableViewController = tNewOrderTableViewController;
self.newOrderTableViewController.orderListViewController=self;
[tNewOrderTableViewController release];
}
if(transferedOrderViewController == nil)
{
TransferedOrderTableViewController *tTransferedOrderTableViewController = [[TransferedOrderTableViewController alloc] init];
self.transferedOrderViewController = tTransferedOrderTableViewController;
[tTransferedOrderTableViewController release];
}
[newOrderTabelView setDataSource:self.newOrderTableViewController];
[newOrderTabelView setDelegate:self.newOrderTableViewController];
[transferedOrderTableView setDataSource:self.transferedOrderViewController];
[transferedOrderTableView setDelegate:self.transferedOrderViewController];
self.newOrderTableViewController.view = self.newOrderTableViewController.tableView;
self.transferedOrderViewController.view = self.transferedOrderViewController.tableView;
UIView *NewOrdeertableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(newOrderTabelView.frame.origin.x, newOrderTabelView.frame.origin.y-30, self.newOrderTabelView.frame.size.width, 30)];
NewOrdeertableHeaderView.backgroundColor = [UIColor colorWithRed:0.0196 green:0.513 blue:0.949 alpha:1.0];
UILabel *NewOrderTableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.newOrderTabelView.frame.size.width/2, 21)];
NewOrderTableHeaderLabel.text = #"New Order";
NewOrderTableHeaderLabel.backgroundColor = [UIColor clearColor];
NewOrderTableHeaderLabel.textColor = [UIColor whiteColor];
NewOrderTableHeaderLabel.textAlignment = UITextAlignmentLeft;
[NewOrdeertableHeaderView addSubview:NewOrderTableHeaderLabel];
newOrderTableViewHeader = NewOrdeertableHeaderView;
[self.view addSubview:newOrderTableViewHeader];
UIView *TransferedOrdertableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(transferedOrderTableView.frame.origin.x, transferedOrderTableView.frame.origin.y-30, self.transferedOrderTableView.frame.size.width, 30)];
TransferedOrdertableHeaderView.backgroundColor = [UIColor colorWithRed:0.0196 green:0.513 blue:0.949 alpha:1.0];
UILabel *TransferedOrderTableHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.transferedOrderTableView.frame.size.width/2, 21)];
TransferedOrderTableHeaderLabel.text = #"Transfered Order";
TransferedOrderTableHeaderLabel.backgroundColor = [UIColor clearColor];
TransferedOrderTableHeaderLabel.textColor = [UIColor whiteColor];
TransferedOrderTableHeaderLabel.textAlignment = UITextAlignmentLeft;
[TransferedOrdertableHeaderView addSubview:TransferedOrderTableHeaderLabel];
transferedOrderTableViewHeader = TransferedOrdertableHeaderView;
[self.view addSubview:transferedOrderTableViewHeader];
[NewOrdeertableHeaderView release];
[NewOrderTableHeaderLabel release];
[TransferedOrdertableHeaderView release];
[TransferedOrderTableHeaderLabel release]; }
-(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
-(void)reloadNewOrderTable
{
[self initializeTableData];
[self.newOrderTableViewController initializeTableData];
[newOrderTabelView reloadData];
}
#end
sorry if the code is messed up. the problem occurs at
NSDate* orderDate = order.Date;
where order comes from SalesOrder which has an NSDate in it, this NSDate is set in another place where the customer confirms the order, the thing is when the customer confirms the order, the date that will be saved in my SalesOrder is correct, but when I'm trying to assign it in the above code, it goes 1 day back... probably I'm making a mistake and missing some simple fact here, thanks in advance for the help.
P.S: I'm also showing the oder date to customer in some other places (list of orders) and the date in there is correct.
Edit: Nice catch Hot Licks, the values I'm getting there are information related to the order, it appears the problem is in below code when I'm assigning the date NSString to NSDate using dateFromString:
[NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY/MM/dd hh:mm:ss"];
NSString *orderDateStr=[NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 2)];
NSDate *orderDate=[formatter dateFromString:orderDateStr];
the orderDateStr shows the correct date of order, but orderDate shows the day before. I've never encounter such a thing before, any idea ? :)

I wonder if your date format covers the +0000 at the end.Try something like [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; before parsing.

Related

When my viewDidDisappear I try to release my table. Still there when I get back to the view

I declare my table like so:
CGRect cgRct = CGRectMake(5, 5, 310, 363);
videoTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
videoTable.backgroundColor = [UIColor clearColor];
videoTable.separatorColor = [UIColor whiteColor];
videoTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
videoTable.dataSource = self;
videoTable.delegate = self;
videoTable.alwaysBounceVertical = NO;
videoTable.bounces = NO;
videoTable.allowsSelection = YES;
//self.videoTable.layer.zPosition = -1;
//table.dragging = NO;
[self.view addSubview:videoTable];
I remove my table when we leave the view
-(void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(#"*viewDidDisappear");
if (videoTable)
{
NSLog(#"removing table");
[videoTable removeFromSuperview];
videoTable.delegate = nil;
[videoTable release];
// self.videoTable = nil;
}
}
When the view loads again there is no sign of the table, but.. it is still there because
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self populateFileList];
if (videoTable)
{
NSLog(#"table still here");
}
else
{
NSLog(#"No table");
}
}
Can anybody explain what it is that I am missing? I want the table completely gone, so I can alloc a new table.
Many Thanks,
-Code
You need to assign nil to the table after you release it, otherwise, the variable still have a value (an old reference to the table object):
[videoTable release];
videoTable = nil;
You can try this:
-(void)viewDidUnload{
[super viewDidUnload];
[self setVideoTable:nil];
}
if (videoTable) is true if videoTable is different from nil. So you need to assign your table to nil after releasing it.

Update Label in Table View

I have a table view where is showed the name of every element of a database. One field is the price of that element.
I use this UILabel to show the sum of all the prices, and it works perfectly.
- (void)viewWillAppear:(BOOL)animated
{
conto = [[NSNumber alloc] initWithDouble:0];
shoppingListItems = [[NSMutableArray alloc] init];
[super viewWillAppear:animated];
[self loadDataFromDb];
[self sortListArray];
[self.tableView reloadData];
if ([conto intValue] < 0) {
walletLabel.textColor = [UIColor redColor];
} else { walletLabel.textColor = [UIColor greenColor]; }
walletLabel.text = [[NSString alloc] initWithFormat: #"Saldo: %#€", [conto stringValue]];
}
"conto" variable is calculate inside "loadDataFromDB" method.
I would like to update it every time I delete a row from the table.
Any suggestion?
Easy. Just call your update routine in your tableView:commitEditingStyle:forRowAtIndexPath: method.

UIView drawRect vs initWithFrame

I have a UIView which has several buttons added to it as subviews. Currently I have the buttons in drawRect:, which I have heard is a bad idea because drawRect can be called several times. I tried to move these UIButtons to initWithFrame, but they just don't get drawn. How should I fix this?
I am not using interface builder, so there are no NIB files present. The initWithFrame is called. I checked by adding an NSLog(...), and my NSMutableArrays are being properly initialized.
My initWitFrame: code currently has very little. It is
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
results = [[NSMutableArray alloc] init];
inputs = [[NSMutableArray alloc] init];
format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"EEE, MMM dd, yyyy"];
renc =[[RenameController alloc] init];
}
return self;
}
I have tried to add a textlabel like the following
#define BOX_WIDTH 155.0
#define BOX_OFFSET 45.00
#define ROW_HEIGHT 27
- (id)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
results = [[NSMutableArray alloc] init];
inputs = [[NSMutableArray alloc] init];
format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"EEE, MMM dd, yyyy"];
renc =[[RenameController alloc] init];
double row=0.0;
[self makelabel:#"Start" at:CGRectMake(0, row, BOX_OFFSET, ROW_HEIGHT)];
}
return self;
}
-(void) makelabel:(NSString *) str at:(CGRect) rect
{
UILabel *title = [[UILabel alloc] initWithFrame:rect];
title.text = str;
title.textAlignment = UITextAlignmentLeft;
title.textColor = [UIColor blackColor];
title.backgroundColor = [UIColor clearColor];
title.font = [UIFont boldSystemFontOfSize:FONT_SIZE];
[self addSubview:title];
[title release];
}
#tonklon is spot on. Additionally, the usual pattern is to have your custom initialization code in a third method, such as -sharedInit, which is called after the appropriate super initializer:
- (id)sharedInit {
// do whatever, possibly releasing self on error and returning nil
return self;
}
- (id)initWithFrame: (CGRect)frame {
self = [super initWithFrame: frame];
if (self) {
self = [self sharedInit];
}
return self;
}
- (id)initWithCoder: (NSCoder *)aDecoder {
self = [super initWithDecoder: aDecoder];
if (self) {
self = [self sharedInit];
}
return self;
}
I bet the view is loaded from a nib file? Just use the same code to add your subviews in initWithCoder:
UIView Class Reference:
If you use Interface Builder to design
your interface, this method is not
called when your view objects are
subsequently loaded from the nib file.
Objects in a nib file are
reconstituted and then initialized
using their initWithCoder: method,
which modifies the attributes of the
view to match the attributes stored in
the nib file. For detailed information
about how views are loaded from a nib
file, see Resource Programming Guide.

Why does my TTTableViewController not appear

I have an application which contains a scrollview with two subviews (to scroll left and right between them)
Both views appear correctly and scrolling between the views worked fine. Now I want to change the first view to be a TTTableView (courtesy of Three20) but when I use the class 'TableControlsTestController' from the TTCatalog application all I see is an empty tableView
Note that this class contains all the data to display 6 cells
To add the new TTTableView I use the following
[scrollView addSubview:detailView.view];
where detailView is the instance of TableControlsTestController
To try and narrow down where the problem is I also tried calling
[self presentModalViewController:detailView animated:YES];
This correctly displays the tableview with the 6 cells.
Why when I try to add the view to the scrollView does this not work as I expect?
For reference if you do not have access to the TTCatalog
#import "TableControlsTestController.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
#implementation TableControlsTestController
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)init {
if (self = [super init]) {
self.tableViewStyle = UITableViewStyleGrouped;
self.autoresizesForKeyboard = YES;
self.variableHeightRows = YES;
UITextField* textField = [[[UITextField alloc] init] autorelease];
textField.placeholder = #"UITextField";
textField.font = TTSTYLEVAR(font);
UITextField* textField2 = [[[UITextField alloc] init] autorelease];
textField2.font = TTSTYLEVAR(font);
textField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
TTTableControlItem* textFieldItem = [TTTableControlItem itemWithCaption:#"TTTableControlItem"
control:textField2];
UITextView* textView = [[[UITextView alloc] init] autorelease];
textView.text = #"UITextView";
textView.font = TTSTYLEVAR(font);
TTTextEditor* editor = [[[TTTextEditor alloc] init] autorelease];
editor.font = TTSTYLEVAR(font);
editor.backgroundColor = TTSTYLEVAR(backgroundColor);
editor.autoresizesToText = NO;
editor.minNumberOfLines = 3;
editor.placeholder = #"TTTextEditor";
UISwitch* switchy = [[[UISwitch alloc] init] autorelease];
TTTableControlItem* switchItem = [TTTableControlItem itemWithCaption:#"UISwitch" control:switchy];
UISlider* slider = [[[UISlider alloc] init] autorelease];
TTTableControlItem* sliderItem = [TTTableControlItem itemWithCaption:#"UISlider" control:slider];
self.dataSource = [TTListDataSource dataSourceWithObjects:
textField,
editor,
textView,
textFieldItem,
switchItem,
sliderItem,
nil];
}
return self;
}
#end
It turns out that some of the events were not being propagated through the scrollView and therefore not handled in the TTTableViewController. To fix this I had to do the following in my scrollView
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[myNewTTViewController viewWillAppear:NO];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[myNewTTViewController viewDidAppear:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[myNewTTViewController viewWillDisappear:NO];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[myNewTTViewController viewDidDisappear:NO];
}
As soon as I did this the table popped into life

UIDatePicker questions regarding displaying

Does anyone know if it's possible to change the minute interval of a UIDatePicker after it's been added to a UIView? Is it possible to show and hide 2 different datePickers? I want change the minuteInterval of the UIDatePicker to 5 and 30 when I click a button. Any sample code would be appreciated.
Yes, everything you asked is possible... here's how:
#import "DateStuffAppDelegate.h"
#implementation DateStuffAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
d = [[[UIDatePicker alloc] init] retain];
d.frame = CGRectMake(0, 0, d.frame.size.width, d.frame.size.height);
d.datePickerMode = UIDatePickerModeTime;
d.countDownDuration = 10.0;
d.minuteInterval = 1;
[window addSubview: d];
d1 = [[[UIDatePicker alloc] init] retain];
d1.frame = CGRectMake(0, d.frame.size.height, d1.frame.size.width, d1.frame.size.height);
d1.datePickerMode = UIDatePickerModeTime;
d1.countDownDuration = 10.0;
d1.minuteInterval = 1;
[window addSubview: d1];
[window makeKeyAndVisible];
}
- (IBAction)doStuff
{
d.minuteInterval = 5;
d1.hidden = !d1.hidden;
}
- (void)dealloc {
[window release];
[super dealloc];
}
#end