I have four buttons. I want that selected button keep highlighted till other button is selected. After that other button gets highlighted and first button unhighlighted.
- (void)doHighlight:(UIButton*)b {
[b setHighlighted:YES];
}
-(IBAction)sizeBtnClicked:(UIButton*)btn{
[self performSelector:#selector(doHighlight:) withObject:btn afterDelay:0];
}
My problem is after selecting other button first button does not unhighlight.please guide me how to do it.
declare a variable button tempBtn in your .h , alloc it and then perform this.
-(IBAction)sizeBtnClicked:(UIButton*)btn{
[tempBtn setHighlighted:NO];
[btn setHighlighted:YES];
tempBtn=btn ;
[self performSelector:#selector(doHighlight:) withObject:btn afterDelay:0];
}
You simply get the reference of last selected button
and then in the sizeBtnClicked, set the highlighted property of last selected button to NO.
In nib file you can change a image for button selection in inspector
Related
This is my code for a back button.
I have 9 buttons (button1,button2...button9)
And i want this button to activate if i press any of the other 9 buttons.
So is there a way where i can do this?
[_backButtonOutlet addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
Create some method with will call all the desired methods that you want to call at the same time and call that method on button tapped event. Good Luck!
-(IBAction) button1:(id)sender
{
[self btn1Pressed:sender];
[self btn2Pressed:sender];
[self btn3Pressed:sender];
[self btn4Pressed:sender];
[self btn5Pressed:sender];
[self btn6Pressed:sender];
[self btn7Pressed:sender];
[self btn8Pressed:sender];
}
_backButtonOutlet is a reference to the button so you don't want it to be called when any buttons are pressed. button1: is the method that you want to be called when any button is pressed.
Option 1:
call:
[otherButtonOutlet_N addTarget:self action:#selector(button1:) forControlEvents:UIControlEventTouchUpInside];
on each of the other buttons (replace otherButtonOutlet_N as appropriate).
Option 2:
In your XIB / storyboard, connect all of the buttons to the button1: method. You can add multiple target/action pairs to a button.
I would like to change the state of a UIButton, only when it appears on a certain page (with currentIndex). But when navigating to the next page, my UIButton shows the same selectedState rather than changing.
Here is my implemented code:
- (IBAction)bookmarkState:(id)sender {
currentIndex = [modelArray indexOfObject:contentViewController.page];
if (_BookmarkState != _bookmarkbtn.tag) {
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-N.png"]
forState:UIControlStateNormal];
[self setBookmarkState:NO];
[contentViewController.bookmarks removeIndex:currentIndex];
}
else {
[_bookmarkbtn setImage:[UIImage imageNamed:#"Bookmark-Y.png"]
forState:UIControlStateNormal];
[self setBookmarkState:YES];
[contentViewController.bookmarks addIndex:currentIndex];
}
}
How can i fix this.
Thanks for help.
Here is what is wrong with your code:
You define currentIndex but neither read again it nor change the array where it comes from.
You are changing an array with bookmarks, but you never read it.
You compare the tag of an object called _bookmarkbtn, presumably a button, with another object that looks like some weird kind of class (initial capital letter) but is probably a constant.
Therefore, you are not using currentIndex, and not comparing anything that refers to the sender of the button action.
Maybe you intended to compare the right element of your bookmarks array to the tag of the button that was pressed? Your code does not do this.
Maybe a straight-forward way is to simply set the correct the image of the button whenever the page appears and just toggle it whenever the button is pressed.
I've got a UIButton that acts as a switch.
When user taps on it, its state changes to "selected" and action "one" is called. When user taps again UIButton state changes to "not selected" and the action is no longer available.
Is there a way to set the UIButton to "selected" by taping on a completely different UIButton and have it change to "selected" and call the same action as well?
Cheers
From what I have understood from the question, I would like to give you my suggestion
if (!<your_button>.selected)
{
<your_button>.selected = YES;
// do your stuff here
}
else
{
<your_button>.selected = NO;
// do your stuff here, in your case action should be no longer available so that,
<your_button>.userInteractionEnabled = NO;
}
Enjoy Programming !
try like this,'
-(IBAction)nextbutton:(id)sender{
UIButton *btn=(UIButton *)[self.view viewWithTag:tagValue];//pass tag value of that particuler button
btn.selected=YES;
//do whatevr you want.
}
simple thing when you tap on the uibutton make an action for that and in the action for that button , you can set the button state to selected and then in the next line of code you can call that method (action)
- (IBAction)btn1Touched:(id)sender {
[_btn2 setSelected:YES];
[self btn2Touched:self];
}
- (IBAction)btn2Touched:(id)sender {
NSLog(#"btn Action called");
}
UIbutton has state called selected and it is managed via property selected
so to make a button to selected state
[buttonInstance setSelected:YES];
To make it unselected use
[buttonInstance setSelected:NO];
Kindly write a function for that button action like,
-(void)Buttonaction
{
do your action here
also set your button has selected state.
buttonname.setselected=yes;
}
call this function while you are tabbing your button.
Do This:
-(IBAction)switchButtonClicked:(UIButton*)sender
{
if(sender.isSelected)
{
.....
}
else
{
...
}
[sender setSelected:!sender.isSelected];//This will work as Switch.
}
set Selected(Switch ON) and default(Switch OFF) property (image and title) of UIButton from XIB, OR from Code as you want.
With a single button you can do you functionality with this code
I have two button in iPhone app I want that if button1 is hidden and when we create PDF then we may check that button1 is already hidden then does not hide it again and also does not show it back when PDF generated because when PDF is created I hide some buttons.
you can write something like below...
if(button1.hidden){
//here button1 is hidden , so you can write code accordingly...
}else{
//here button1 is not hidden , so you can write code accordingly...
}
happy coding!!!!!
You can just use the .hidden property on the button
To hide the button, if not already hidden
if(!yourButton.hidden) //Button is not hidden
{
//hide button
yourButton.hidden = YES;
//do other magic here if required
}
To change the visibility of your button, you can use
yourButton.hidden = YES; //Set button hidden, (YES or NO, NO is by default)
you can check that UIButton is Hidden or not with its isHidden Property and also Hide that UIButton with its setHidden: method.
use this bellow logic with its method
if ([button1 isHidden]) {
//Generate your PDF here because if Button is hide then this condition true and load this code
}
else{
[button1 setHidden:YES];//we hide that button here and after load your code here
}
I have made a horizontal scroll view with images and buttons in it using a for loop, what i want to do now is when a button is pressed, open the image it corresponds to full screen. The issue im having is determining which button has been pushed. I am using :
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then :
-(void)buttonPressed:(UIButton *)sender {}
What can i do to fix this? Thanks
Which button has been pushed
Create a property to store a tag. In the buttonPressed method set the property to sender.tag
Now you can know which was the button who got pressed last.
The sender is the button that the user has tapped, so when you create the buttons you could use the tag property and set it to an index of an array where you hold your images. But this could be a bit unstable if you change the order or amount of images or buttons for example so be prepared to check for that.
one way to do so is to keep references of your button (with a property, an attribute in your class) and test if it is the good one in your buttonPressed method
- (void)buttonPressed:(UIButton *)sender {
if (sender == self.myButton) {
// DO YOUR WORK HERE
}
}
you can also create a method for this and only this one button
by the way it is better to say
- (IBAction)buttonPressed:(UIButton *)sender
you then can set the target of your button in the interface builder interface
set tag for each button in the for loop but.tag=i;
-(void)buttonPressed:(UIButton *)sender {
if (sender.tag==1){
//display image 1
}
else if ....
}