I have written this code to see different image states...
UIButton *btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
btnComment.tag=indexPath.row;
[btnComment addTarget:self action:#selector(goToComment:)forControlEvents:UIControlEventTouchDown];
UIImage *img1 = [UIImage imageNamed:#"commentbtndown.png"];
UIImage *img2 = [UIImage imageNamed:#"commentbtnup.png"];
UIImage *img3 = [UIImage imageNamed:#"commentbtnover.png"];
[btnComment setImage:img1 forState:UIControlStateNormal];
[btnComment setImage:img2 forState:UIControlStateHighlighted];
[btnComment setImage:img3 forState:UIControlStateSelected];
[btnComment setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];
btnComment.frame =CGRectMake(0, 100, 95, 25);
[cell addSubview:btnComment];
[img1 release];
[img2 release];
[img3 release];
but its not working, it is always showing me image 1.
p.s. I have added these images in the table view cell
The problem is that you are creating the UIImage objects with an autorelease method imageNamed, and you are releasing these objects afterwards, which cause your button to have invalid objects and because of that the images will not be displayed
Try removing this lines of code and your button will work
[img1 release];
[img2 release];
[img3 release];
And also, if you want the button to receive the touch events, you will have to add it to the contentView of your cell object, otherwise the button will be shown but you will not be able to tap it.
[cell.contentView addSubview:btnComment]
Well one problem with your code is that you should not be releasing those image variables. imageNamed: returns an autoreleased UIImage. I'm doubt that this is causing your problem, though.
Try using | instead of + for your fourth setImage call.
Another problem with your code is where you add the button to your UITableViewCell. Instead of this:
[cell addSubview:btnComment];
You should add subviews to your cell's contentView:
[cell.contentView addSubview:btnComment];
But I'm also not certain that this may cause your problem...
Related
I have an UI button and it works correctly when I pressed.
But if I press the button three times, I get an EXc_BAD_ACCESS error.
I thought I release something in somewhere but I couldn't find the solution.
Could you please help me?
Kind regards.
This is the function when I pressed the button. And in dealloc I release them. When I am tracking, it doesn't give the error in function. I got it after function, but I dont know where the code goes after this function.
- (IBAction) doSomething: (id)sender
{
[self.answerDict replaceObjectAtIndex:currentPageNumber withObject:#"1"];
[self.b setImage:nil forState:UIControlStateNormal];
[self.c setImage:nil forState:UIControlStateNormal];
[self.d setImage:nil forState:UIControlStateNormal];
[self.e setImage:nil forState:UIControlStateNormal];
UIImage *img = [UIImage imageNamed:#"a.jpg"];
[self.a setImage:img forState:UIControlStateNormal];
[img release];
}
UIImage *img = [UIImage imageNamed:#"a.jpg"];
[self.a setImage:img forState:UIControlStateNormal];
[img release];
[img release]; is the problem. You are releasing an object which you dont own. img in this case is auto-released.
Remove [img release]; and see if the crash occurs
I suggest you to comment the code line by line and in that way you will understand what is the purpose of BAD_ACCESS error. At firs close whole code in the doSomething: may be the main reason is in your button ...
I am using following code:
slidingImageIconView=[[UIScrollView alloc]init];
[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];
UIButton *iconImageSlide=[[UIButton alloc] init];
[iconImageSlide setImage:[UIImage imageNamed:#"dummy_img.jpg"] forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];
but my button is not adding in scroll view, i know i am missing something very silly but please help me..
I notice that you have slidingImageIconView and slidingImageIconScrollView. Is that a typo? I've left it like that in my examples here.
You need to initWithFrame in order to set the button position and size:
slidingImageIconView=[[UIScrollView alloc]init];
[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];
UIButton *iconImageSlide=[[UIButton alloc] initWithFrame:CGRectMake(0,0,100,20)];
[iconImageSlide setImage:[UIImage imageNamed:#"dummy_img.jpg"] forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];
If you want to make the button the same size as the image - then the following rearrangement would allow that. Also note that I've used setBackgroundImage instead of setImage. I tend to use setImage to put an 'icon' on the button, but setBackgroundImage for the actual button-image.
UIImage* buttonImage = [UIImage imageNamed:#"dummy_img.jpg"];
slidingImageIconView=[[UIScrollView alloc]init];
[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];
UIButton *iconImageSlide=[[UIButton alloc] initWithFrame:CGRectMake(0,0,buttonImage.size.width,buttonImage.size.height)];
[iconImageSlide setBackgroundImage:buttonImage forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];
You haven't set the frame of the buttonf it has no useful geometry.
In code, I set the following:
btnLogo.SetImage(UIImage.FromBundle("images/Zouak_logo_button.png"), UIControlState.Normal);
btnLogo.SetImage(UIImage.FromBundle("images/Zouak_logo_button_pushed.png"), UIControlState.Selected);
I couldn't find a UIControlState.Pressed or Pushed or anything along those lines. When the button is pushed of course it isn't showing the version of the image that I want. Do I need to do this in the Click event manually?
I think your syntax is kind of strange, but I guess that you want to do:
UIButton *btn = [[UIButton alloc] init];
[btn setImage:uiimg forState:UIControlStateHighlighted];
Do this for all states:
UIImage *newNormalImage = [UIImage imageNamed:#"images/Zouak_logo_button.png"];
[btnLogo setBackgroundImage:newNormalImage forState:UIControlStateNormal];
// Image for highlighted state
UIImage *newHighlightedImage = [UIImage imageNamed:#"mages/Zouak_logo_button_pushed.png"];
[btnLogo setBackgroundImage:newHighlightedImage forState:UIControlStateHighlighted];
// Image for selected state
UIImage *newSelectedImage = [UIImage imageNamed:#"mages/Zouak_logo_button_pushed.png"];
[btnLogo setBackgroundImage:newSelectedImage forState:UIControlStateSelected];
This is something I don't understand. Look at this method (copied from http://blog.blackwhale.at/2009/07/uibutton-in-uitableview-footer/)
See that footerView is allocated on the beginning of the code and never released.
As far as I understand, the object should be autoreleased or at least released after used on a regular case, but as this method is run automatically by the tables when they are built, where exactly will this view be released...
or will it leak? I have seen Apple samples of code with stuff like that, so I suppose the object is being released somewhere... but where?
// custom view for footer. will be adjusted to default or specified footer height
// Notice: this will work only for one section within the table view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if(footerView == nil) {
//allocate the view if it doesn't exist yet
footerView = [[UIView alloc] init];
//we would like to show a gloosy red button, so get the image first
UIImage *image = [[UIImage imageNamed:#"button_red.png"]
stretchableImageWithLeftCapWidth:8 topCapHeight:8];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setBackgroundImage:image forState:UIControlStateNormal];
//the button should be as big as a table view cell
[button setFrame:CGRectMake(10, 3, 300, 44)];
//set title, font size and font color
[button setTitle:#"Remove" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(removeAction:)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
}
//return the view for the footer
return footerView;
}
thanks.
Seeing as footerView is an instance variable on this class, this code is correct. footerView is not autoreleased (by you, anyway) so it will continue to exist as long as you don't release it (because you "own"/retained it by allocating it). The proper place to do that would be in this class's dealloc method.
As long as you do that, this code looks correct to me. :)
footerView is an instance variable. Is it released in the dealloc of the class?
I added an image to button
UIImage* deleteImage = [UIImage imageNamed:#"Delete.png"];
CGRect imageFrame=CGRectMake(-4,-4, 310, 55);
[btn setFrame:imageFrame];
btn.backgroundColor=[UIColor clearColor];
[btn setBackgroundImage:deleteImage forState:UIControlStateNormal];
[btn setTitle:#"Delete" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(editDeleteAction) forControlEvents:UIControlEventTouchUpInside];
[elementView addSubview:btn];
[deleteImage release];// do we need to release the image here
If I release here its working fine but in object allocations no.of image count is increasing.
If you create an image with the imageNamed: message you don't have to release it, because you get an autoreleased image back.
Only if you create the image with one of the init...: messages you have to release it later on.
"imageNamed:" method gives an autoreleased object, so you don't have to release object.
FYI: "imageNamed:" method uses an internal cache (you may face memory warning if imageNamed: method used extensively). It is better to remove cache on receiving warnings.
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
[[ImageCache sharedImageCache] removeAllImagesInMemory];
}
Go through this guide http://akosma.com/2009/01/28/10-iphone-memory-management-tips/