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;
}
}
Related
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];
}
on one viewController of my app there is a long list of 20 or so buttons added programmatically, all which i want to call the same method, but to identify themselves through their button tag, but i ran into a problem that has set me back a few hours of research and attempts.The basic problem is i dont quite know how to access a programmatically created button in any other method than the method that they were initialized in.
My questions summerized:
1) if i were to create the button in the viewDidLoad method, how can I access it in a void method that i create?
2) How can i access those button tags in the created void method?
Here is the code that i have so far, but it is producing errors that ill explain below.
-(void)viewDidLoad{
float itemScrollerXdirection =0;
float itemScrollerYdirection =0;
float ySize =70.0;
float xSize = 70.0;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(itemScrollerXdirection,itemScrollerYdirection,xSize,ySize);
[button addTarget:self action:#selector(itemSelected) forControlEvents:UIControlEventTouchUpInside];
button.tag =1;
[button setTitle:#"button1" forState:UIControlStateNormal];
[itemScroller addSubview:button];
}
//no errors in the above code
-(void)itemSelected{
if ([sender tag] == 1) { //Gets error "Use of undeclaired identifier 'sender'"
button.hidden = YES; //Gets error "Use of undeclaired identifier 'button1'"
}
}
We aren't working in the mystical relm of ruby, things need to be initialized and stored somewhere inorder for you to call them, try this:
#.h
#interface MyController : UIViewController{
NSMutableArray *buttons;
}
#.m
-(void)init // Or whatever you use for init
{
buttons = [[NSMutableArray alloc] init];
}
-(void)viewDidLoad{
//blah blah (what you already have)
[button addTarget:self action:#selector(itemSelected:) //Add ":"
forControlEvents:UIControlEventTouchUpInside];
button.tag =0;
[buttons addObject:button] //Add button to array of buttons
//blah blah (what you already have)
}
-(IBAction)itemSelected:(id)sender{
UIButton* button = [buttons objectAtIndex:sender.tag]
button.hidden = YES;
}
Note: I'm doing this from memory on, so it might not work perfectly.
#.h
#interface MyController : UIViewController{
UIButton *buttons;
}
#.m
-(void)viewDidLoad{
float itemScrollerXdirection =0;
float itemScrollerYdirection =0;
float ySize =70.0;
float xSize = 70.0;
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(itemScrollerXdirection,itemScrollerYdirection,xSize,ySize);
[self.button addTarget:self action:#selector(itemSelected) forControlEvents:UIControlEventTouchUpInside];
self.button.tag =1;
[self.button setTitle:#"button1" forState:UIControlStateNormal];
[itemScroller addSubview:button];
}
//no errors in the above code
-(void)itemSelected
{
if ([sender tag] == 1)
{
self.button.hidden = YES;
}
}
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.
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.
how do i make these buttons so that only one can be used at a time? Im not getting any errors right now when i run btw. Im just looking for a solution to my challenge. Thanks for any help
they are generated in a for loop like this:
for (int l=0; l<list.length; l++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:l];
CGRect buttonRect = CGRectMake(11+charact*20, -40 + line*50, 18, 21);
aButton.frame = buttonRect;
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:#" " forState:UIControlStateNormal];
[gameScroll addSubview:aButton];
}
And then the action for when a button is clicked is:
- (void) buttonClicked:(UIButton *)sender {
int tag = sender.tag;
if (sender.selected == TRUE) {
[sender setSelected:FALSE];
[sender setBackgroundColor:[UIColor clearColor]];
}
else if (sender.selected == FALSE) {
[sender setSelected:TRUE];
[sender setBackgroundColor:[UIColor redColor]];
}
}
right now everything works but i want it to know if theres already a button selected and deselect that other button, or else to automatically deselect any time the user clicks outside of the range of that button
thanks in advance
I would suggest to put all ur buttons to Array in your button initialisation
NSMutableArray* buttons = [NSMutableArray arrayWithCapacity: list.length];
for (int l=0; l<list.length; l++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:l];
CGRect buttonRect = CGRectMake(11+charact*20, -40 + line*50, 18, 21);
aButton.frame = buttonRect;
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:#" " forState:UIControlStateNormal];
[gameScroll addSubview:aButton];
[buttons addObject: aButton];
}
and every time buttonClicked triggered, then do your logic:
for (UIButton* button in buttons)
{
if (button != sender)
{
[button setSelected: FALSE];
[button setBackgroundColor:[UIColor redColor]];
}
}
int tag = sender.tag;
if (sender.selected == TRUE) {
[sender setSelected:FALSE];
[sender setBackgroundColor:[UIColor clearColor]];
}
else if (sender.selected == FALSE) {
[sender setSelected:TRUE];
[sender setBackgroundColor:[UIColor redColor]];
}
hope helps :)
You can store currently selected button in a separate variable and deselect it in buttonClicked: method:
- (void) buttonClicked:(UIButton *)sender {
int tag = sender.tag;
currentButton.selected = NO;
if (currentButton != sender){
currentButton = sender;
currentButton.selected = YES;
}
else{
currentButton = nil;
}
}
Also you can specify background colour for each state in button itself so you actually don't need to change it each time manually
If you also want to deselect your button when user just touches the screen you can implement touchesEnded:withEvent: in your view controller and reset currentButton in it (that method will get called if no other control intercept the touch event - so it may not be sufficient in all cases)
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event{
currentButton.selected = NO;
currentButton = nil;
}