I have created scroll view and sets the buttons are in the scroll view. The Buttons are scrolling horizontally and it works fine. If i clicked the button, i set background image as "Selected State" in button. My problem is how can i changed the selected state in different button, when clicking it and how can i deselected the "selected state" button when clicking the another button.
I have three buttons in the scroll view,
-(IBAction) Button1 : (id) sender
{
// btn1.selected = YES;
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateSelected];
}
-(IBAction) Button2 : (id) sender
{
// btn2.selected = YES;
[btn2 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateSelected];
}
-(IBAction) Button3 : (id) sender
{
// btn3.selected = YES;
[btn3 setImage:[UIImage imageNamed:#"three.png"] forState:UIControlStateSelected];
}
see the below image,(Health, Entertainment and Money Watch are the three buttons)
Image http://www.freeimagehosting.net/uploads/6b3daab12f.png
and
Img http://www.freeimagehosting.net/uploads/b6e0f234dc.png
Note:(Like, Tabbar and Segmented control)
On clicking first button and sets background image in selected state and clicking the second button, then first buttons are to be deselected. So how can i maintain the selected state, till another button is clicked.
Thanks in Advance.
I solved this task in the following way:
init method:
Create number of buttons with defined images for normal and selected state.
Assign tag for each button (for example, for i'th button tag is 1000+i).
Assign IBAction for each button.
action method:
Remove selection from previously selected button (search it by it's tag with [view viewWithTag:] method)
Select sender.
Save sender's tag.
Here's the code:
- (void)init {
....INITIALIZE SCROLLVIEW HERE.....
for ( int i = 0; i < 10; i++ ) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:.....];
[btn setImage:_IMAGE_ forState:UIControlStateNormal];
[btn setImage:_IMAGE2_ forState:UIControlStateSelected];
[btn setTag:i + 1000];
[btn addTarget:self action:#selector(setSelectedButton:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
}
- (IBAction)setSelectedButton:(id)sender {
[self setSelectedButtonByIndex:((UIButton *)sender).tag - 1000];
}
- (void)setSelectedButtonByIndex:(NSInteger)index {
if ( selectedElemId >= 0 ) {
UIButton *btn = (UIButton *)[self viewWithTag:selectedElemId + 1000];
[btn setSelected:NO];
}
UIButton *btn = (UIButton *)[self viewWithTag:index + 1000];
[btn setSelected:YES];
selectedElemId = btn.tag - 1000;
}
Related
I have a UIScrollView full of custom UIButtons that are generated programmatically.
This code executes every iteration through the loop, typically 7 times.
[cardButton
addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[cardButton setTag:i + 100];
[self.scrollView addSubview:cardButton];
Elsewhere I have this function:
- (IBAction) buttonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
}
How do I link the two? My button actually stores all the information I need from it inside its label so I really just need to detect when it is being tapped so I can respond.
You already linked two with this line
[cardButton
addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
Sender is returning the instance of button which you pressed.so
- (IBAction) buttonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"%d", [button tag]);
switch (button.tag) {
case 1:
//Action for button with tag 1
break;
case 2:
//Action for button with tag 2
break;
default:
break;
}
}
I want to do a shift key. When I click it, the key will change to other letters. How can I make it bright when clicking this shift button like in iphone ? Can you give some example codes ?
If you'd like a button's image to change when tapped, and then back, you can use the -[setImage:forState:] method. For this example, imagine yourself having 2 images: image_one.png and image_two.png for the unpressed and pressed images respectively.
Suppose you have a button like so:
UIButton *button = [[UIButton alloc] init];
[button setFrame:CGRectMake(0, 0, 100, 44)];
[button setImage:[UIImage imageNamed:#"image_one.png"] forState:UIControlStateNormal];
[[self view] addSubview:button];
You can add a target and action that will change the image back and forth. To see which image it is currently, I'd use the tag property.
[button setTag:0]; // initial, non pressed button tag, for this example
[button addTarget:self action:#selector(swapImage:) forControlEvents:UIControlEventTouchUpInside];
And have something like the following in the setImage method:
-(void)swapImage:(UIButton *)sender {
if ([sender tag] == 0) {
[sender setTag:1]; // pressed tag, for example
[sender setImage:[UIImage imageNamed:#"image_two.png"] forState:UIControlStateNormal];
} else {
[sender setTag:0]; // non-pressed tag, for example
[sender setImage:[UIImage imageNamed:#"image_one.png"] forState:UIControlStateNormal];
}
}
When you first tap the button, it selects. Tapping it another time will unselect it - just like the keyboard's shift button.
I have an UIButton, I need to change button's image when button is tapped. Button's background images are set in ViewDidLoad method. Pls refer images attached on this thread.
When I tap on button first time, button property will be changed to selected and "arrow_right.png" image will be set to our Button.
2.On second tap, I observed that when button state is highlighted, But it's background image is not setting. You can see some blur effect instead.
My concern is I saw while toggling UIButton, image is not getting set for highlighted state. when button's state changing from Selected to Normal.
Is it a bug or my mistake?
Thank you.
- (void)viewDidLoad
{
[super viewDidLoad];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_normal.png"] forState:UIControlStateNormal];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateSelected];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateHighlighted];
}
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
button.selected = !button.selected;
if (button.selected)
{
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateSelected];
}
}
try this
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
if(button.selected) {
[button setSelected:NO]
} else {
[button setSelected:YES]
}
}
i diddn't understand why you need to set image again, all you need to do is set its selected property , button will behave as you expected
I am using this code for displaying Images in scroll view... In this buttons are created with the help of for loop... and then set image for every buttons... Now i want to select multiple images... I want when i click on particular button, its image replace with "tick image" and when i again pressed on it, replace with original image means show unchecked..
for(int i=0; i<[imageArray count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}
buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[buttonImage setImage:[arrayOfImages objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:#selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;
}
I tried this code for image changed on state change in (buttonImagePressed) method...
[buttonImage setImage:[UIImage imageNamed:#"Checkmark.png"] forState:UIControlStateSelected];
but it doesn't work... and it change only image of last button every time instead of particular clicked button.... I also tried to hide the button but it again hide only last button. where i m doing wrong???
there is any another way to change it??? please help me
Instead of UIControlStateSelected you should use UIControlStateNormal and keep some bool value which shows whether that button is previously selected or not or you can set selected property of button to true or false based on selection say:
-(void) buttonImagePressed:(UIButton*)sender
{
UIButton *button = sender;
if(button.selected) //already selected so now it should be deselected
{
[button setImage:[UIImage imageNamed:#"UnCheckmark.png"] forState:UIControlStateNormal];
button.selected = false;
}
else
{
[button setImage:[UIImage imageNamed:#"Checkmark.png"] forState:UIControlStateNormal];
button.selected = true;
}
}
For this, Take a global flag variable as BOOL Flag = NO;
Set tag for each button also.
In the method
-(IBAction)buttonImagePressed:(UIButton *)sender{
if(flag==NO){
flag=YES;
[sender setImage:[UIImage imageNamed:#"Checkmark.png"] forState:UIControlStateNormal];
}
else{
flag=NO;
[sender setImage:[arrayOfImages objectAtIndex:sender.tag] forState:UIControlStateNormal];
}
}
Say for example i have two volume buttons ( + and - )
How can I implement something such as when holding the + button, it will raise up the volume incrementally at an interval? (I'm only interested in doing an action at an interval while the button is being pressed)
You can use a timer for this. Start the timer when the touch starts. If the timer expires, increase or decrease the volume and restart the timer. When the touch ends, cancel the timer.
Assuming you have two button one for - and other for + ,
you could store the interval information in the tag field of your button
Installing the interval value in your button tag property.
myPulseButton.tag = 10;
myMinusButton.tag = 10;
Adding action with your button.
[myPulseButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
Implement the buttonEvent method like below.
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
First add the button...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(increase)forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
then write the method for buttton press.
-(void)increase
{
//increase the volume here
}
if it is not the solution you want means add the comment...