Remove Subview of UIButton - iphone

im running the same piece of code over and over when the user presses a button. If the user presses the button an image appears over the button, if they press it again the image is removed from the buttons subview.
here is my code:
UIImageView *overlay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"OverlayImage.png"]];
NSString *senderstag = [NSString stringWithFormat:#"%i", buttonFromSender.tag];
if([[dictonaryOfImagesToDelete allKeys] containsObject:senderstag]){
[[buttonFromSender subviews]makeObjectsPerformSelector:#selector(removeFromSuperview)];
}
else{
[buttonFromSender addSubview:overlay];
}
NSString *imageName = [[NSString alloc]init];
imageName = [arrayWithImageNames objectAtIndex:buttonFromSender.tag];
[imagesToDelete addObject:imageName];
[dictonaryOfImagesToDelete setObject:imagesToDelete forKey:[NSString stringWithFormat:#"%i", buttonFromSender.tag]];
Im using a dictionary to keep track of which buttons have been pressed. instead of the imageview over the button being removed, it removes the whole button. How can i get it just to remove the imageview and not the button?
Thanks :D

I have ended up making the UIImageview of the overlay, a UIButton. This allowed to set actions for it. Here is the code that is used:
UIImage *image = [UIImage imageNamed:#"OverlayImage.png"];
UIButton *overlay = [[UIButton alloc]init];
overlay.frame = CGRectMake(0, 0, 100, 150);
[overlay setImage:image forState:UIControlStateNormal];
[overlay addTarget:self action:#selector(removeSelectedImage:) forControlEvents:UIControlEventTouchUpInside];
overlay.tag = buttonFromSender.tag;
[buttonFromSender addSubview:overlay];
NSString *imageName = [[NSString alloc]init];
imageName = [arrayWithImageNames objectAtIndex:buttonFromSender.tag];
[imagesToDelete addObject:imageName];
[dictonaryOfImagesToDelete setObject:imageName forKey:[NSString stringWithFormat:#"%i", buttonFromSender.tag]];
this set up the new UIButton
then to get rid of the UIButton i did this:
-(IBAction)removeSelectedImage:(id)sender{
UIButton *button = (UIButton *)sender;
[button removeFromSuperview];
NSString *tag = [NSString stringWithFormat:#"%i", button.tag];
NSString *deleteWhat = [[NSString alloc]init];
deleteWhat = [dictonaryOfImagesToDelete objectForKey:tag];
[imagesToDelete removeObject:deleteWhat];
[dictonaryOfImagesToDelete removeObjectForKey:tag];
}

If you want to display different image for a button state u can directly initialize the button with to load a picture in that state.
say for example Enabled/Disabled, or Selected/Deselected(normal)
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:[UIImage imageNamed:#"My Image"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"My Other Image"] forState:UIControlStateSelected];
[myButton setBackgroundImage:[UIImage imageNamed:#"BG Img"] forState:UIControlStateNormal];
References:
Background Image:
http://developer.apple.com/library/IOS/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/backgroundImageForState:
Button Image:
http://developer.apple.com/library/IOS/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setImage:forState:

Related

Adding activity indicator to a custom button not working?

I have an application in which i need to add activity indicators above the buttons whenever it has been clicked.I made all the custom buttons like this and add it to navigation bar.`
UIButton *btnNext1 = [UIButton buttonWithType:UIButtonTypeCustom];
btnNext1.frame = CGRectMake(100, 100,38, 38);
UIImage *image = [UIImage imageNamed:#"default.png"];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
imageView.frame = CGRectMake(10,5,38,38);
[imageView.layer setCornerRadius:5.0];// position it to the middle
[btnNext1 setBackgroundImage:imageView.image forState:UIControlStateNormal];
[imageView release];
[btnNext1 addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];
self.navigationItem.leftBarButtonItem = btnNext;
[btnNext release];
then in the sender methode i tried
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];
// Add to button
[button addSubview:myIndicator];
// Start the animation
[myIndicator startAnimating];
But it is not showing up. Can anybody point me where I am going wrong?
Try this in button action
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myIndicator setCenter:button.center];
[button addSubview:myIndicator];
[button bringSubviewToFront:myIndicator];
[myIndicator startAnimating];
I tried with your code it working fine may be the image and indicator both are same color or you are calling different method ,otherwise it's working fine.
once check below images.
before and after button actions
Change your sender method to
UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];
// Add to button
[self addSubview:myIndicator];
// Start the animation
[myIndicator startAnimating];
You need to add indicator to uiview rather than button.

Setting UIImage as background of a UIButton

Whats wrong with the following piece of code:
UIButton *button = [[UIButton alloc]init];
CGRect frame = CGRectMake(180, 10, 33, 33);
button.frame = frame;
button.tag = 1001;
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIImage imageNamed:#"icon.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[button release];
If this is wrong where does it need correction and why?
I see several problems:
You sent autorelease to image, then manually released it.
You released button, but you didn't add it as a subview to anything. So basically, you did all that for nothing.
You instantiate UIImage, then you do instantiate it again. in the next line: Instead just do:
UIImage *image = [UIImage imageNamed:#"icon.png"];
and delete UIImage *image = [[[UIImage alloc]init]autorelease]; and [image release]; statement.
You can try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(180, 10, 33, 33);;
button.tag = 1001;
UIImage *image = [UIImage imageNamed:#"icon.png"];
if (image == nil) {
NSLog(#"can't find icon.png");
} else {
[button setBackgroundImage:image forState:UIControlStateNormal];
}
//add button to the parent's view
Did you make sure image is not nil?

adding reflection to iCarousel

I tried to add reflection to my icarousel. Firstly my code was like this and it was working wright.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton* button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
return button;
}
After changing, my code became like this:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (button == nil)
{
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
[view update];
return view;
}
Reflection appears. But the problem is when i move the carousel view, images not flowing correctly. Sometimes another carousel view appears.
Do you have any idea what causes that?
Code should be:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (view == nil)
{
//no view available to recycle, so create new one
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.bounds;
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 99;
[view addSubview:button];
}
else
{
button = (UIButton *)[view viewWithTag:99];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
[button setBackgroundImage:image forState:UIControlStateNormal];
[view update];
return view;
}
I use images with reflection built in them... don't need to do anything in code that way.. :p
I found my mistake. I am loading images here:
-(void) getImagesWithThread{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//for(int i=0;i<bookCount;i++){
for(int i=0;i<bookCount;i++){
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [arrBookImages objectAtIndex:i]]];
UIImage *image = [[UIImage imageWithData: imageData] retain];
image = [self scaleToSize:CGSizeMake(140, 200) withImage:image];
if(image!=nil) [arrBookImages replaceObjectAtIndex:i withObject:image];
[image release]; }
[carousel reloadData];
[pool drain];
}
When i performed this in background it didn't reload correctly. İf i perform normally, it works. But i want to load them in a thread. I mean first i want to set images and replace them images from url. but i want them to appear one by one. Do you have any idea?

Way to change icon of accessory type in UITableviewcell

I have table view which is showing list of files to download. When I tap the accessory button it will download the selected file. I want to change image of detail disclosure button. Is it possible....?
And If I use Button with image in accessory view. Is there any table delegate method for this...
Answer2: You can make your own method and call that in that case.
int row=indexPath.row;
UIButton *trackImageOnMap=[[UIButton alloc] initWithFrame:CGRectMake(420, 9, 40, 50)];
[trackImageOnMap setImage:[UIImage imageNamed:#"track_map_icon.png"] forState:UIControlStateNormal];
int iId=[[self.imageId objectAtIndex:row] intValue];
NSLog(#"iId=%d",iId);
[trackImageOnMap setTag:iId];
[trackImageOnMap addTarget:self action:#selector(trackImageOnMapButtonTouched:)forControlEvents:UIControlEventTouchDown];
[trackImageOnMap setContentMode:UIViewContentModeScaleToFill];
//[cell setAccessoryView:trackImageOnMap];
[cell addSubview:trackImageOnMap];
You could create a UIImageView and assign it to the UITableViewCell's accessoryView.
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"accessoryIcon"]] autorelease];
cell.accessoryView = imageView;
If you want a button for the accessoryView it's basically the same:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"buttonImage"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(someAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag = cell.indexPath;
cell.accessoryView = button;
If you want to replace a default type, you can overwrite UITableViewCell and use the following code:
- (void)setAccessoryType:(UITableViewCellAccessoryType)accessoryType
{
if (accessoryType == UITableViewCellAccessoryDisclosureIndicator)
{
self.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YourNewImage.png"]] autorelease];
}
else
{
[super setAccessoryType:accessoryType];
}
}

Can't get programmatically made UIButton to work - iPhone

Here is the code I'm using:
-(void)viewDidLoad
{
NSString *checkerPath = [[NSBundle mainBundle] pathForResource:#"black-yellow-checker" ofType:#"png"];
UIImage *checkerImage = [[UIImage alloc] initWithContentsOfFile:checkerPath];
checkerView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 230.0f, 320.0f, 280.0f)];
[checkerView setImage:checkerImage];
UIButton *backButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
backButton.frame = CGRectMake(45.0f, 175.0f, 230.0f, 50.0f);
[backButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"yellowButton" ofType:#"png"]] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goBackHome:) forControlEvents:UIControlEventTouchDown];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:#"Marker Felt" size:30];
backButton.titleLabel.textColor = [UIColor blackColor];
[checkerView addSubview:backButton];
}
- (void)goBackHome:(id)sender
{
NSLog(#"go back pressed");
}
Not sure why this doesn't work. The button shows up, but when I press it nothing happens. It doesn't even change to the "indented" image when I touch it. Nothing. Can't use IB, has to be programmatically. Any thoughts?
UIImageView has userInteractionEnabled set to NO by default. This prevents any subviews from getting touches.
You could either enable user interaction of the image view by
checkerView.userInteractionEnabled = YES;
or create a new UIView to include both checkerView and the backButton.
UIView* newView = ...
...
[newView addSubview:checkerView];
[newView addSubview:backButton];
I believe that the forControlEvents: has to be UIControlEventTouchUpInside.