How to add Customized View in UIAlertView - iphone

I have created a View containing labels and buttons with Controller.
By using addSubView method my view appears but button does not respond to view controller.. Can anybody help me doing what I want to do??
I have used this code to add view as subview in my alert view.
[myAlertView addSubview:myViewController.view];

You need to add an action on your button like below.
[yourButton addTarget:self action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
- (IBAction) myAction
{
//Action to perform when yourButton pressed.
}
Hope this helps you!

myview=[[UIView alloc] init];
[myview setFrame:CGRectMake( 50.0f, 50.0f, 120.0f, 120.0f)];
[UIView commitAnimations];
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.7];
[myview setFrame:CGRectMake( 90.0f, 20.0f, 280.0f, 170.0f)];
myview.backgroundColor=[UIColor whiteColor];
//myview.frame = CGRectMake(10, 10, 200, 250);
[self.view addSubview:myview];
UIColor *forBackground = [UIColor colorWithPatternImage:[UIImage imageNamed:#"apple_bg.png"]];
[myview setBackgroundColor:forBackground];
myview.layer.cornerRadius=20;
myview.clipsToBounds=YES;
myview.layer.borderWidth = 2;
myview.layer.borderColor = [[UIColor whiteColor] CGColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 10.0, 160.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.scrollEnabled=NO;
mytext.scrollEnabled=NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = #"First label";
[mytext release];
[myview addSubview:mytext];
entered = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 15.0, 150.0, 25.0)];
[entered setBackgroundColor:[UIColor whiteColor]];
entered.layer.cornerRadius = 10;
entered.textAlignment = UITextAlignmentCenter;
entered.clipsToBounds = YES;
[entered addTarget:self action:#selector(textDidChange:)forControlEvents:UIControlEventEditingChanged];
[myview addSubview:entered];
UITextView *mytext2 = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 50.0, 160.0, 25.0)];
mytext2.backgroundColor = [UIColor clearColor];
mytext2.textColor = [UIColor blackColor];
mytext2.editable = NO;
mytext2.scrollEnabled=NO;
mytext2.font = [UIFont systemFontOfSize:15];
mytext2.text = #"Second label";
[myview addSubview:mytext2];
[mytext2 release];
entered1 = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 55.0, 150.0, 25.0)];
[entered1 setBackgroundColor:[UIColor whiteColor]];
entered1.layer.cornerRadius = 10;
entered1.textAlignment = UITextAlignmentCenter;
entered1.clipsToBounds = YES;
[myview addSubview:entered1];
[entered1 release];
UITextView *mytext3 = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 90.0, 160.0, 25.0)];
mytext3.backgroundColor = [UIColor clearColor];
mytext3.textColor = [UIColor blackColor];
mytext3.editable = NO;
mytext3.scrollEnabled=NO;
mytext3.font = [UIFont systemFontOfSize:15];
mytext3.text = #"Third Label";
[myview addSubview:mytext3];
[mytext3 release];
entered2 = [[UITextField alloc] initWithFrame:CGRectMake(120.0, 95.0, 150.0, 25.0)];
[entered2 setBackgroundColor:[UIColor whiteColor]];
entered2.layer.cornerRadius = 10;
entered2.clipsToBounds = YES;
entered2.text=#"Active";
entered2.textAlignment = UITextAlignmentCenter;
[myview addSubview:entered2];
[entered2 release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(EditTable:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Ok" forState:UIControlStateNormal];
button.frame = CGRectMake(60.0, 130, 60, 30);
[myview addSubview:button];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self action:#selector(EditTable1:)
forControlEvents:UIControlEventTouchDown];
[button1 setTitle:#"Cancel" forState:UIControlStateNormal];
button1.frame = CGRectMake(150.0, 130, 60, 30);
[myview addSubview:button1];
[self.view addSubview: myview];

Related

how to add items in uinavigationbar at a specific position?

I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.
What i want is in following images
in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar.
There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.
Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.
So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..
you can add Multiple UIlabel or Image as look like bellow image:-
this can do using bellow code you can change Label and imageView frame and put as your requirement
- (void)viewDidLoad
{
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"a59117c2eb511d911cbf62cf97a34d56.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
}
You can add subviews directly to the navigationBar -
UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];
yourLabel frame origin will be relative to the navigation bar
Try this code
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame: CGRectMake(160, 7 , 40, 30)];
[button0 setTitle:#"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:#"Btn_Sort_Btn_Sort_Places.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:#"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
You can set an arbitrary view instead of a view controller's title:
myViewController.navigationItem.titleView = someView;
Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];
[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
Have a look at this piece of code. With some modifications it should solve your problem:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:#"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];
Just modify the values in the CGRectMake() to fit your needs.

create button,text field and insert images programmatically in Xcode

I am facing trouble unexpected issue in iphone app. for whatever reason, I compile this, I get a SIGABRT error. it happens on the:
textFieldRounded=[[UITextField alloc]initWithframe: CGRectMake(20,50,280,31)];
textFieldRounded.borderStyle=UITextBorderStyleRoundedRect;
textFieldRounded.textColor=[UIColor blackColor];
textFieldRounded.font=[UIFont systemFontOfSize:17.0];
textFieldRounded.placeholder=#"enter your name";
textFieldRounded.backgroundColor=[UIColor whiteColor];
textFieldRounded.autocorrectionType=UITextAutocorrectionTypeNo;
textFieldRounded.backgroundColor=[UIColor clearColor];
textFieldRounded.keyboardType=UIKeyboardTypeDefault;
textFieldRounded.returnKeyType=UIReturnKeyDone;
textFieldRounded.clearButtonMode=UITextFieldViewModeWhileEditing;
[self.view addSubView:textFieldRounded];
textFieldRounded.delegate=self;
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button" forstate:UIControlStateNormal];
button.frame = CGRectMake(20,108,97,37);
[self.view addSubview:button];
label= [[UILabel alloc] initWithFrame:CGRectMake(40,243,240,21)];
label.backgroundColor=[UIColor clearColor];
label.textAlignment,UITextAlignmentCenter;
label.text=#"Label";
[self.view addSubview:label];
CGRect myImageRect = CGRectMake(40.0f,190.0f,240.0f,128.0f);
image= [[UIImageView alloc] initWithFrame:myImageRect];
[image setImage:[UIImage imageNamed:#"Default#2x.png"]];
image.backgroundColor=[UIColor clearColor];
image.opaque =YES;
image.alpha=0;
[self.view addSubview:image];
[image release];
}
you can Creat UITextField, UIButton or UILabel like this bellow code:-
UITextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
UIButton
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"UIButton" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
[button release];
UILabel
UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 240, 300, 30)];
[self.view addSubview:headingLabel];
// Optional
headingLabel.text = #"WELCOME";
headingLabel.textColor = [UIColor orangeColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont fontWithName:#"MarkerFelt-Thin" size:14.0];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blueColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;
UIImageView
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
myImage.image = [UIImage imageNamed:#"myImage.png"];
[self.view addSubview:myImage];
[mymyImage release];
Hope its helps you

UIButton does not respond to touchupinside when in a uipopover

I added a button to my UIPopoverController but it appears to not respond to touches. I don't know if I am supposed to set some property on the UIPopoverController or what. Here is the code that renders the popover view and button.
- (void)topicImageButtonPressed
{
CGRect aFrame = CGRectMake(0.0, 0.0, 1000.0, 600.0);
UIViewController *aView = [[UIViewController alloc] init];
aView.view.frame = aFrame;
UIImageView *iView = [[UIImageView alloc] init];
[iView setContentMode:UIViewContentModeScaleAspectFit];
[iView setImage:self.topicImageView1.image];
aView.view = iView;
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextButton addTarget:self
action:#selector(quizButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[nextButton setTitle:#"Next" forState:UIControlStateNormal];
nextButton.frame = CGRectMake(700.0, 550.0, 160.0, 40.0);
[aView.view addSubview:nextButton];
//aView.view.backgroundColor = [UIColor colorWithPatternImage:self.topicImageView1.image];
imagePopoverController = [[UIPopoverController alloc]
initWithContentViewController:aView];
imagePopoverController.popoverContentSize = CGSizeMake(1000, 600);
imagePopoverController.passthroughViews = [NSArray arrayWithObject:nextButton];
[imagePopoverController presentPopoverFromRect:CGRectMake(212, 10, 1000, 600) inView:self.view
permittedArrowDirections:0 animated:YES];
}
Adding this made it work.
iView.userInteractionEnabled = YES;
UIViewController *popoverContent = [[UIViewController alloc]init];
UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
datepiker= [[UIDatePicker alloc]init];
[datepiker setFrame:CGRectMake(0, 0, 320, 216)];
datepiker.datePickerMode=UIDatePickerModeDateAndTime;
datepiker.hidden=NO;
datepiker.minimumDate=[NSDate date];
[self.view addSubview:datepiker];
[datepiker release];
// [datepiker addTarget:self action:#selector(changedDate:) forControlEvents:UIControlEventValueChanged];
btn_add=[[UIButton alloc]initWithFrame:CGRectMake(115, 250, 100, 30)];
[btn_add setTitle:#"Add" forState:UIControlStateNormal];
[btn_add setFont:[UIFont fontWithName:#"Arial-BoldMT" size:20]];
[btn_add setBackgroundColor:[UIColor redColor]];
[btn_add addTarget:self action:#selector(AddDate:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:btn_add];
btn_cancel=[[UIButton alloc]initWithFrame:CGRectMake(115, 300, 100, 30)];
[btn_cancel setTitle:#"Cancel" forState:UIControlStateNormal];
[btn_cancel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:20]];
[btn_cancel setBackgroundColor:[UIColor redColor]];
[btn_cancel addTarget:self action:#selector(CancelDate:) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:btn_cancel];
[popoverView addSubview:datepiker];
[popoverView addSubview:btn_add];
popoverContent.view = popoverView;
popoverContent.contentSizeForViewInPopover = CGSizeMake(320,350);
self.popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(230,250, 320,220)
inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[popoverView release];
[popoverContent release];
-(void)AddDate:(id)sender
{
}
-(void)CancelDate:(id)sender
{
[popoverController dismissPopoverAnimated:YES];
}
try this .. it works for me successfully.

UIToolBar is not shown when adding the same toolbar as inputAccessaryView

I have got a uipickerview which will be populated when pressing a uibutton.
This uipickerview has a uitoolbar control in the top of the uipickerview.
That tool bar has got uibuttons in it.It is working fine until i do the below thing.
I want to have that uittoolbar, on top of the uikeyboard.So I have added , that uitoolbar control as InputAccessoryView.
When i did this, the uitoolbar comes with uikeyboard.that is fine.
but when i tap on the button, which has to show the uipickerview with that toolbar and also needs to resign the toolbar.
So when tap on the button, i have resigned the uikeyboard.
the uipickerview is showing but the toolbar is not visible.
I have been trying to fix this for a long time, but really facing a tough time.
Please let me know.
- (void)viewDidLoad
{
[super viewDidLoad];
// Create an UIButton
self.btnClick = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.btnClick addTarget: self action:#selector(showPicker)
forControlEvents:UIControlEventTouchUpInside];
// Set frame width, height
self.btnClick.frame = CGRectMake(10, 100, 30, 30);
self.btnClick.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.view addSubview:self.btnClick];
self.toolBar = [[[UIToolbar alloc] init]autorelease];
[self.toolBar sizeToFit];
self.toolBar.frame = CGRectMake(0,198, 320, 35);
self.toolBar.barStyle = UIBarStyleBlackTranslucent;
UIImage *image = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.bounds = CGRectMake( 0, 0, image.size.width, 25 );
[aButton setImage:image forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(pickerNumPadClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *numButton = [[[UIBarButtonItem alloc] initWithCustomView:aButton]autorelease];
UIImage *aCenterimage = [UIImage imageNamed:#"button_normal.png"];
UIButton *myCenterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myCenterBtn setTitle:#"Poet Names" forState:UIControlStateNormal];
[myCenterBtn setBackgroundImage:aCenterimage forState:UIControlStateNormal];
myCenterBtn.frame = CGRectMake(100, 0,200,25);
myCenterBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
myCenterBtn.titleLabel.font = [UIFont systemFontOfSize:10];
myCenterBtn.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
myCenterBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
myCenterBtn.bounds = CGRectInset(myCenterBtn.bounds, -3, 2);
myCenterBtn.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[myCenterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myCenterBtn addTarget:self action:#selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithCustomView:myCenterBtn]autorelease];
UIImage *sendImage = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *Sendbutton = [UIButton buttonWithType:UIButtonTypeCustom];
Sendbutton.frame = CGRectMake(0, 0,50,25);
[Sendbutton setTitle:#"Send" forState:UIControlStateNormal];
[Sendbutton setBackgroundImage:sendImage forState:UIControlStateNormal];
Sendbutton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
Sendbutton.titleLabel.font = [UIFont systemFontOfSize:10];
Sendbutton.titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
Sendbutton.titleLabel.shadowOffset = CGSizeMake(0, -1);
Sendbutton.bounds = CGRectMake( 0, 0, 50, 25 );
Sendbutton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
[Sendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[Sendbutton addTarget:self action:#selector(pickerDoneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc ] initWithCustomView:Sendbutton]autorelease];
[self.toolBar setItems:[NSArray arrayWithObjects:numButton,flex, doneButton, nil]];
[self.view addSubview:self.toolBar];
self.txtView = [[UITextView alloc] init];
self.txtView.layer.cornerRadius = 15;;
self.txtView.clipsToBounds = YES;
self.txtView.frame = CGRectMake(45, 100, 274, 98);
self.txtView.layer.borderWidth = 1.0f;
self.txtView.delegate = self;
self.txtView.scrollEnabled = YES;
self.txtView.backgroundColor = [UIColor whiteColor];
self.txtView.contentInset = UIEdgeInsetsZero;
self.txtView.returnKeyType = UIReturnKeyDone; // type of the return key
self.txtView.layer.borderColor = [[UIColor blueColor] CGColor];
self.txtView.font = [UIFont fontWithName:#"Helvetica" size:17.0f];
[self.txtView setInputAccessoryView:self.toolBar];
[self.view addSubview:self.txtView];
self.itemsArray = [[[NSArray alloc]initWithObjects:#"William Langland",#"Philip Larkin", #"Dorianne Laux", #"David Lehman", #"Denise Levertov", nil]autorelease];
}
-(void)showPicker
{
[self.txtView resignFirstResponder];
self.pickerView = [[[UIPickerView alloc]init]autorelease];
self.pickerView.showsSelectionIndicator = YES;
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.pickerView.tag = 0;
self.pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
[self.pickerView sizeToFit];
self.pickerView.frame = CGRectMake(0, 293 , 320, 15);
self.toolBar.frame = CGRectMake(0,253, 320, 35);
[self.view addSubview:self.toolBar];
[self.view addSubview:self.pickerView];
[self.pickerView selectRow:1 inComponent:0 animated:YES];
}
Try this link
http://blog.swierczynski.net/2010/12/how-to-create-uipickerview-with-toolbar-above-it-in-ios/
It shows how to put a inputAccessoryView on uipicker

-[CALayer release]: message sent to deallocated instance

I'm having a problem with some code in the loadView: method of one of my view controllers. Essentially I have a view which centres itself in a larger view (on an iPad) and has some labels, buttons and icons which are programmatically loaded into it.
The problem occurs when the view controller calls the dealloc method, and tries to release. I get a -[CALayer release]: message sent to deallocated instance error and the application crashes.
From reading up about this error, it appears I am over-releasing something, but I don't think that's the case: the code below is the trouble maker and I've tried both autorelease and manual release calls on everything, as well as simply not releasing them (I know, I know, that's bad) but it continues to cause havok.
container = [[UIView alloc] initWithFrame:CGRectNull];
container.autoresizingMask = ( UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin );
container.frame = CGRectMake( (viewSize.width-320)/2, (viewSize.height-480)/2, 320, 480);
[self.view addSubview:container];
UILabel *welcomeTo = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
welcomeTo.backgroundColor = [UIColor clearColor];
welcomeTo.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:18.0];
welcomeTo.frame = CGRectMake(23, 15, 103, 24);
welcomeTo.shadowColor = [UIColor blackColor];
welcomeTo.shadowOffset = CGSizeMake( 0, 1 );
welcomeTo.text = #"Welcome to";
welcomeTo.textAlignment = UITextAlignmentLeft;
welcomeTo.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:welcomeTo];
UILabel *progressions = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
progressions.backgroundColor = [UIColor clearColor];
progressions.font = [UIFont fontWithName:#"HelveticaNeue-BoldItalic" size:44.0];
progressions.frame = CGRectMake(20, 30, 280, 56);
progressions.shadowColor = [UIColor blackColor];
progressions.shadowOffset = CGSizeMake( 0, 1 );
progressions.text = #"Progressions";
progressions.textAlignment = UITextAlignmentCenter;
progressions.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:progressions];
UILabel *blurb = [[[UILabel alloc] initWithFrame:CGRectNull] autorelease];
blurb.backgroundColor = [UIColor clearColor];
blurb.font = [UIFont fontWithName:#"HelveticaNeue" size:14.0];
blurb.frame = CGRectMake(23, 100, 274, 51);
blurb.numberOfLines = 3;
blurb.shadowColor = [UIColor blackColor];
blurb.shadowOffset = CGSizeMake( 0, 1 );
blurb.text = #"Kick off the fun by adding some content, or if you're not sure what you're doing, check out the \"Getting Started\" guide!";
blurb.textAlignment = UITextAlignmentLeft;
blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:blurb];
UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 170, 280, 88)];
[button1 setBackgroundImage:[UIImage imageNamed:#"emptyButtonLarge.png"] forState:UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"emptyButtonLargeHighlighted.png"] forState:UIControlStateHighlighted];
[button1 addTarget:self action:#selector(createChart:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:button1];
[button1 release];
UIImageView *icon1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"addchart.png"]];
icon1.frame = CGRectMake(30, 180, icon1.frame.size.width, icon1.frame.size.height);
[container addSubview:icon1];
[icon1 release];
UILabel *button1title = [[[UILabel alloc] initWithFrame:CGRectMake(110, 185, 180, 20)] autorelease];
button1title.backgroundColor = [UIColor clearColor];
button1title.font = [UIFont boldSystemFontOfSize:17.0];
button1title.shadowColor = [UIColor blackColor];
button1title.shadowOffset = CGSizeMake( 0, 1 );
button1title.text = #"Create a Chart";
button1title.textAlignment = UITextAlignmentLeft;
button1title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button1title];
UILabel *button1blurb = [[[UILabel alloc] initWithFrame:CGRectMake(110, 205, 180, 40)] autorelease];
button1blurb.backgroundColor = [UIColor clearColor];
button1blurb.font = [UIFont systemFontOfSize:13.0];
button1blurb.numberOfLines = 2;
button1blurb.shadowColor = [UIColor blackColor];
button1blurb.shadowOffset = CGSizeMake( 0, 1 );
button1blurb.text = #"A cheat sheet for a displaying a song in any key.";
button1blurb.textAlignment = UITextAlignmentLeft;
button1blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button1blurb];
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(20, 270, 280, 88)];
[button2 setBackgroundImage:[UIImage imageNamed:#"emptyButtonLarge.png"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:#"emptyButtonLargeHighlighted.png"] forState:UIControlStateHighlighted];
[button2 addTarget:self action:#selector(createSet:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:button2];
[button2 release];
UIImageView *icon2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"addset.png"]];
icon2.frame = CGRectMake(30, 280, icon2.frame.size.width, icon2.frame.size.height);
[container addSubview:icon2];
[icon2 release];
UILabel *button2title = [[[UILabel alloc] initWithFrame:CGRectMake(110, 285, 180, 20)] autorelease];
button2title.backgroundColor = [UIColor clearColor];
button2title.font = [UIFont boldSystemFontOfSize:17.0];
button2title.shadowColor = [UIColor blackColor];
button2title.shadowOffset = CGSizeMake( 0, 1 );
button2title.text = #"Create a Set";
button2title.textAlignment = UITextAlignmentLeft;
button2title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button2title];
UILabel *button2blurb = [[[UILabel alloc] initWithFrame:CGRectMake(110, 305, 180, 40)] autorelease];
button2blurb.backgroundColor = [UIColor clearColor];
button2blurb.font = [UIFont systemFontOfSize:13.0];
button2blurb.numberOfLines = 2;
button2blurb.shadowColor = [UIColor blackColor];
button2blurb.shadowOffset = CGSizeMake( 0, 1 );
button2blurb.text = #"A collection of charts that can be displayed like a book.";
button2blurb.textAlignment = UITextAlignmentLeft;
button2blurb.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button2blurb];
UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(20, 370, 280, 40)];
[button3 setBackgroundImage:[UIImage imageNamed:#"emptyButtonSmall.png"] forState:UIControlStateNormal];
[button3 setBackgroundImage:[UIImage imageNamed:#"emptyButtonSmallHighlighted.png"] forState:UIControlStateHighlighted];
[button3 addTarget:self action:#selector(gettingStarted:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:button3];
[button3 release];
UILabel *button3title = [[[UILabel alloc] initWithFrame:CGRectMake(20, 370, 280, 36)] autorelease];
button3title.backgroundColor = [UIColor clearColor];
button3title.font = [UIFont boldSystemFontOfSize:17.0];
button3title.shadowColor = [UIColor blackColor];
button3title.shadowOffset = CGSizeMake( 0, 1 );
button3title.text = #"Getting Started";
button3title.textAlignment = UITextAlignmentCenter;
button3title.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:button3title];
UILabel *version = [[[UILabel alloc] initWithFrame:CGRectMake(20, 420, 280, 20)] autorelease];
version.backgroundColor = [UIColor clearColor];
version.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:12.0];
version.shadowColor = [UIColor blackColor];
version.shadowOffset = CGSizeMake( 0, 1 );
version.text = [NSString stringWithFormat:#"Version v%# (%#)", [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"], kRevisionNumber];
version.textAlignment = UITextAlignmentCenter;
version.textColor = [UIColor colorWithWhite:1.0 alpha:0.6];
[container addSubview:version];
The container property is released in the dealloc method, as follows:
- (void)dealloc {
[leather release];
[backButton release];
[container release];
[super dealloc];
}
Any answers or suggestions people can provide would be greatly appreciated! I am truly stumped.
Profile your application in the simulator with 'Zombies' Instrument.
Run your app for a while and do whatever you have to do to make your app crash. When it does, you will get a pop up like the image below and it will halt the profiling of the app:
Then if you click on the little arrow next to the address (0x158b3c00) .. it will take you to the object retain/release history for the object that was over released (the zombie).
If you highlight the line above where the retain count went to -1, and open View -> Extended detail, it should point you to the stack trace and line in your code where the object was overreleased:
If you double click the class where it is occuring, it will open up your source and show you the bad line of code:
The over-releasing could be occurring during the objects life time and not just when they're being created. A few tips:
use accessors to ensure that pointers are set to nil
be consistent with your use of release/autorelease. Personally I prefer to have [[[Class alloc] init] autorelease] all on 1 line.
Learn the difference between release/autorelease. Let's Build NSAutoreleasePool is a good place to start.