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.
Related
In my application i set some animations in View1's view load() ,At first launch its working propely,if im pushed into otherview ,and while pop into view1 its not animating,What shoud i do for animate while pop view from other view?here my view did load code,
- (void)viewDidLoad
{
artext=[[NSMutableArray alloc] init];
arimage=[[NSMutableArray alloc] init];
arsound=[[NSMutableArray alloc] init];
[super viewDidLoad];
[self copysqlitetodocuments];
[self Readthesqlitefile];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"classroom.png"]];
}
i tried with
- (void)viewDidAppear:(BOOL)animated
{
[self buttonmover];
[super viewDidAppear:animated];
}
its not working,Can any one pls help me to solve this problem
If you want to execute code when the view appears (e.g. an animation), -viewDidLoad is the wrong place to do it.
You should probably use:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// ...
}
Try in viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
artext=[[NSMutableArray alloc] init];
arimage=[[NSMutableArray alloc] init];
arsound=[[NSMutableArray alloc] init];
[self copysqlitetodocuments];
[self Readthesqlitefile];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"classroom.png"]];
}
When your viewDidLoad method is called, the controller isn't yet in the view hierarchy, so the animations doesn't work.
You have to perform animations in viewWillApper, after implementing the method [super viewWillAppear]. Something like:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
artext = [[NSMutableArray alloc] init];
arimage = [[NSMutableArray alloc] init];
arsound = [[NSMutableArray alloc] init];
[self copysqlitetodocuments];
[self Readthesqlitefile];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"classroom.png"]];
}
I have app to create a graph i have created a view based app and then add the code for creating graph in it but it does not disply the graph. If use same code to create a separate UIView then it works other wise not
#import <UIKit/UIKit.h>
#import "ECGraph.h"
#import "ECGraphItem.h"
#class GraphsViewController;
#interface Display : UIView {
NSArray *percentages;
int myY;
ECGraph *graph;
ECGraphItem *item1;
ECGraphItem *item2;
}
#property(nonatomic,retain)NSArray*percentages;
-(void) setPercentageArray:(NSArray*) array;
#end
#import "Display.h"
#import "ECGraph.h"
#implementation Display
#synthesize percentages;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef _context = UIGraphicsGetCurrentContext();
graph = [[ECGraph alloc] initWithFrame:CGRectMake(500,-320,320, 200) withContext:_context isPortrait:NO];
item1 = [[ECGraphItem alloc] init];
item2 = [[ECGraphItem alloc] init];
/*
ECGraphItem *item1 = [[ECGraphItem alloc] init];
ECGraphItem *item2 = [[ECGraphItem alloc] init];*/
item1.isPercentage = YES;
item1.yValue=myY;
item1.width = 35;
item1.name = #"item1";
item2.isPercentage = YES;
item2.yValue =17;
item2.width = 35;
item2.name = #"item2";
[graph setXaxisTitle:#"name"];
[graph setYaxisTitle:#"Percentage"];
[graph setGraphicTitle:#"Histogram"];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]];
NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,nil];
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
I am adding this view in GraphsViewController but its not showing anything
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self createGraph];
percentages = [NSArray arrayWithObjects:#"80",#"17", nil];
display = [[Display alloc] init];
[self.view addSubview:display];
[display setPercentageArray:percentages];
}
You should do the drawing in a separate UIView object and add it as a subview to your view controller's view. That's the way it is supposed to work.
The UIView class uses an on-demand drawing model for presenting
content.
Source: View Programming Guide for iOS
as opposed to
The UIViewController class provides the fundamental view-management
model for all iOS apps. ... A view controller manages a set of views
that make up a portion of your app’s user interface.
Source: UIViewController Class Reference
Edit:
// ...
display = [[Display alloc] init];
CGRect dFrame = CGRectMake(50, 50, 320, 200); // change these to whatever values you need
[display setFrame:dFrame];
[self.view addSubview:display];
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.
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
I've double checked all the connections in the nib file. My code -
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"iphone_bg_login.png"]];
self.title = #"Login screen";
loginTxt = [[UITextField alloc] init];
pwdText = [[UITextField alloc] init];
loginFailedTxt = [[UILabel alloc] init];
loginBtn = [[UIButton alloc] init];
navAppDelegate = (NavAppDelegate *)[[UIApplication sharedApplication] delegate];
navAppDelegate.navController.navigationBarHidden = YES;
//NSArray *subVs = (NSArray *) [self.view subviews];
[super viewDidLoad];
}
I've used a subclass of UIView (UIControl) and added all the UI elements to it in the Interface builder.The UIControl's touchDown method is connected to backgroundTap method.
-(IBAction) backgroundTap:(id) sender {
[loginTxt resignFirstResponder];
[pwdText resignFirstResponder];
//[[UIApplication sharedApplication] becomeFirstResponder];
//[sender resignFirstResponder];
}
So the keyboard isn't removed like it's supposed to. Not sure why.
Thanks for the help!
Teja.
DyingCactus has pointed to your error. You're replacing the NIB-version of the control with a completely different control, losing your pointer to the one in the NIB. When you call resignFirstResponder, you're calling it on your duplicate object, not the one that's actually on the screen. Get rid of the alloc and init calls for things wired in the NIB.