add two background image for selected and unselected UISegments iphone - iphone

I have two segment buttons (1 and 2) I want to add a background image one for selected segment and another image for unselected segment.How can I do that?
Here is what I have so far,should I set the background image here?:
- (void)selectWithSegment:(UIButton *)sender
{
for (UIButton *button in self.segments) {
if (button == sender) {
if (self.staySelected) {
button.backgroundColor = [UIColor colorWithRed:17.0/255.0 green:17.0/255.0 blue:17.0/255.0 alpha:1.0];
[button setBackgroundImage:nil forState:UIControlStateNormal];
}
if ([self.delegate respondsToSelector:#selector(segmentControl:didSelectSegment:)]) {
[self.delegate segmentControl:self didSelectSegment:sender];
}
} else {
[self decorateButton:button];
}
}
}
how can I set image1.png and image2.png for for selected and unselected segments?Can someone help me with the implementation?

I don't know any other way to handle this situation, but i think this can help you:
After making IBOutlet of UISegmentControl set IBAction to valueChange event with below action:
-(IBAction)selectedSegment:(id)sender{
int totalNumberOfSegment = 5;
UISegmentedControl *tmpSig = (UISegmentedControl *)sender;
NSLog(#"Inside sender method");
for (int i = 0; i < totalNumberOfSegment; i++) {
[tmpSig setImage:[UIImage imageNamed:#"unselect.png"] forSegmentAtIndex:i];
}
[tmpSig setImage:[UIImage imageNamed:#"select.png"] forSegmentAtIndex:tmpSig.selectedSegmentIndex];
}

Related

How to check the clicked button title is equal to the image name in uiimageview

Here my code to show the 4 button titles which one image view, I want to show the clicked alert if clicked button title is equal to the UIImageview image. Here is my code,
imageary=[[NSMutableArray alloc]initWithObjects:#"dear.jpg",#"donkey.jpg",#"elephant.jpg",#"fox.jpg",#"giraffe.jpg",#"goat.jpg",#"buffallo.jpg",#"bull.jpg",#"cow.jpg",#"crocodile.jpg", nil]; // declare array of names not images
- (NSString *) convertToDisplayName:(NSString *)actual
{
return [actual stringByDeletingPathExtension]; //return image name without extension
}
Button method
-(IBAction)methodset:(id)sender
{
int i=0;
for(UIView *view in self.view.subviews)
{
if([view isKindOfClass:[UIButton class]])
{
mybutton = (UIButton *)view;
if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
{
i = rand() % [imageary count]; // set random image
animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]];
name=[self convertToDisplayName:[imageary objectAtIndex:i]];
[mybutton setTitle:name forState:UIControlStateNormal];
NSLog(#"current image name :%#",name);
i++;
}
}
}
}
Now im facing problem with
rand(), while shuffling the some values in the button are
repeating. How can i shuflle the values without repeating and show in
UIButton.
Where should I compare for the image name and clicked button title
is equal or not. Please help me to resolve this issue. Please do the
needful.
Use a while loop for the rand bit
while (i != mybutton.tag){
i = rand() % [imageary count];
}
Just compare the imagery names and uibutton title after the if(mybutton.tag ...etc etc because you want it in the for loop right? also indent your code :P. makes it easier to read.
if ([btn.titleLabel.text isEqualToString:#"imageName"])
according to your code
if ([btn.titleLabel.text isEqualToString: name])
You can simply do it by changing the function like:
-(IBAction)methodset:(id)sender
{
UIButton *mybutton = (UIButton *)sender;
if(mybutton.tag == 1||mybutton.tag == 2||mybutton.tag == 3||mybutton.tag == 4)
{
for(int loop = 0; loop<4;loop++)
{
animage.image=[UIImage imageNamed:[imageary objectAtIndex:i]];
name=[self convertToDisplayName:[imageary objectAtIndex:i]];
[mybutton setTitle:name forState:UIControlStateNormal];
NSLog(#"current image name :%#",name);
if ([mybutton.titleLabel.text isEqualToString:[imageary objectAtIndex:i]])
{
//titles equal
}
UIView *newView = [self.view viewWithTag:i];
if([newView isKindOfClass:[UIButton class]])
{
UIButton *newButton = (UIButton *)newView;
[newbutton setTitle:name forState:UIControlStateNormal];
}
}
}
}

How to check if a uiimage view is added on a UIButton

I have taken all the images from photo gallery in an array and then shown all these images on UIButtons and putt them in scroll View to show a grid view.Now when user click the button it should look like as he selected that image, for that I have taken a tick mark image and added it in a UIImageView and added that image view on the button on its click. But now when user again click the same button then how to remove that UIImageview from button when user has already selected multiple images.
Here is my code
-(void)loadImagesOnScrollView
{
scrollView.contentSize=CGSizeMake(0, 55*[assets count]);
int x=0,y=0;
for (int i=0; i<[assets count]; i++) {
ALAsset *asset = [assets objectAtIndex:i];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(x, y, 100, 100);
btn.tag=i;
[btn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnClkd:) forControlEvents:UIControlEventTouchUpInside];
if(i==0)
{
}
else if(i%2 ==0)
{
x=0;
y=y+110;
}
else
{
x=x+110;
}
[scrollView addSubview:btn];
}
}
-(void)btnClkd:(UIButton*)sender
{
if (sender.selected) {
sender.selected=FALSE;
}
else
{
sender.selected=TRUE;
}
if(sender.selected)
{
[sender addSubview:imgView];
}
else
{
}
}
Along this I have one more question, how to set the content size of scroll view so as it can be adjusted with any number of images.
when you add an image on a button on selection, set its tag = constant (say 9999) + sender.tag.
Now again on click of button check if subview with tag 9999+sender.tag exists. If yes, remove that subview from superview
Create a BOOL variable in your .h file eg:
BOOL isButtonClicked; // in .h file
then in your .m file
-(IBAction)onButtonClick:(UIButton *)checkBoxButton
{
if (!isButtonClicked) {
[checkBoxButton setImage:[UIImage imageNamed:#"tickMark.png"] forState:UIControlStateNormal];
isButtonClicked=YES;
}
else
{
[checkBoxButton setImage:[UIImage imageNamed:#"NormalImage.png"] forState:UIControlStateNormal];
isButtonClicked=NO;
}
}
To check if UIImageView is added to an UIButton use the following..
for check if the button has any UIImage
if ( [button.currentImage isEqual:[NSNull null]]) {
NSLog(#"image exist");
}
or
for check if the button has any background Image
if ( [button.currentBackgroundImage isEqual:[NSNull null]]) {
NSLog(#"background image exist");
}

manage multiple pressed UIButtons

I have a uiviewcontroller with many UIButtons that are meant to be selected and kept pressed until touched on again. I define each button in the viewDidLoad and give all of them the same selector method (tapButton):
[button1 addTarget:self action:#selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:#selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
...
What I would like to do, is in the tabButton: method, to use the selector to determine which button was pressed, and then change its state with the following:
- (IBAction) tapButton:(id)sender
{
if ( sender.selected ) {
sender.highlighted = NO;
sender.selected = NO;
} else {
sender.highlighted = YES;
sender.selected = YES;
}
}
You will notice that this is merely a pseuodo code since I can't really do "sender.selected" or "sender.highlighted" but thats what I am trying to accomplish.
is there any in way in which I can accomplish this? I would hate to create 30 "tapButton" methods (thats the number of UIButtons I have, yes...) for managing each UIButton's state.
Thanks a bunch!
You can set the tag of each button like this
button1.tag = 1;
button2.tag = 2;
....
Then in your selector
- (IBAction) tapButton:(id)sender
{
switch((UIButton*)sender.tag){
case 1:
.....
}
}
UIButton * selectedButton = [[UIButton allo]init];
/*for removing old highlight*/
selectedButton.higlighted = NO;
selectedButton = sender;
/*for setting new button highlight*/ sender.selected = YES
the above code will help you . Here am using new button to store last button state
You can use switch case for this...or you can also implement this by using alpha property...
-(IBAction) tapButton:(id)sender
{
UIButton *btn = (UIButton *)sender;
if ([btn isSelected])
{
btn.selected = NO;
btn.alpha = 0.5;
} else {
btn.selected = YES;
btn.alpha = 1;
}
}

UIButton disable and enable background image

My application consist of several buttons , when user touch one of them a shadow appears below them, and the rest of buttons should not have any background image , I need something like Instagram application effects , here is my code but my problem is when I touched other button the shadow does not appears .
#define BGB [btn setBackgroundImage:[UIImage imageNamed:#"shadow.png"] forState:UIControlStateNormal]
#define _BGB [btn setBackgroundImage:nil forState:UIControlStateNormal]
- (IBAction)effectsPerform:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn == EB0) { BGB; } else { _BGB; }
if (btn == EB1) { BGB; } else { _BGB; }
if (btn == EB2) { BGB; } else { _BGB; }
//and other buttons ...
}
Try this way:
-(void)changeButton:(UIButton *)button background:(UIImage *)image
{
[button setBackgroundImage:image forState:UIControlStateNormal];
}
- (IBAction)effectsPerform:(UIButton *)sender
{
[self changeButton:sender background:(sender == EB0 || sender == EB1 || sender == EB2) ? [UIImage imageNamed:#"shadow.png"] : nil];
}
UPDATE:
You do it with another approach, in your viewDidLoad: method, for every button you can set:
[button setBackgroundImage:nil forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"shadow.png"] forState:UIControlStateSelected];
And then in your -(IBAction)effectsPerform:
- (IBAction)effectsPerform:(UIButton *)sender
{
button1.isSelected = (sender == button1);
button2.isSelected = (sender == button2);
....
}
...this way you guarantee you will have only one selected button. What can you do to optimize your code is you can subclass UIButton and in the init method you call the background setter methods, and then all buttons can be of this type (you can mark them in the interface builder by setting their class) - this way you won't need to set the background images for every state for every button.

UIButton and actions problem

I am creating some UIbutton dynamically. And a user click any one of the buttons will display something (ex: different views). So I successfully made the buttons, get the tags. But in the IBAction method below, because all these buttons are dynamically created. So I can't use if, else if statement to show the view based on the tag number. I am thinking of using loops? any ideas?
Here is my code:
NSMutableArray *buttonsArray = [[NSMutableArray alloc] initWithObjects:nil];
for(int i = 0; i < [someArray count]; i++)
{
button = [[UIButton alloc] initWithFrame:CGRectMake(btnX,btnY,btnW,btnH)];
button.tag = i;
[buttonsArray addObject:button];
[[buttonsArray objectAtIndex:i] addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.text = [NSString stringWithFormat:#"Click it"];
[self.view addSubview:button];
btnY = btnY + 120;
}
`-(IBAction) buttonPressed:(id)sender `
{
UIButton *btn = (UIButton *)sender;
NSLog(#"%ld", btn.tag);
//Don't know the number of buttons, so this is not gonna work
if( btn.tag == 1)
{
//do something
}
if( btn.tag == 2)
{
//do something
}
if( btn.tag == 3)
{
//do something
}
if( btn.tag == 4)
{
//do something
}
}
loop is not the perfect solution here i think...agree with Jennis...use switch instead of if else statement...if you want to use loop try:
-(IBAction) buttonPressed:(id)sender{
UIButton *selectedbtn = (UIButton *)sender;
for (UIButton *bttn in buttonsArray) {
if ([bttn.tag == selectedbtn.tag ) {
//do something
}
}
}
you get Multiple UIbutton ? b'coz your X and Y are same for all..
All is OK in your code except two things.
(1) Change the coordinates of buttons so you get different position for all the buttons in the view.
(2) Change the following code:
[[buttonsArray objectAtIndex:i] addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
with this code:
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Let me know if any further help required.