just wondering how to make a button "START" that when pressed triggers a function and the text changes to "STOP". then when pressed again the function will stop and the text changes back to "START"
Ive got the button already that starts the function. and i can handle changing the title, just not sure on what to use to make the 1 button have 2 functions
Add the IBAction method like:
- (IBAction)buttonTapped:(id)sender
{
UIButton *btn = (UIbutton *)sender;
NSString *title=btn.titleLabel.text;
if ([title isEqualToString:#"Start"])
{
//Start
}else
{
//Stop
}
}
Please try this:
- (IBAction) buttonAction:(id)sender
{
if([[(UIButton *)sender currentTitle]isEqualToString:#"START"])
{
[actionButton setTitle:#"STOP" forState:UIControlStateNormal];
//start the action here and change the button text to STOP
}
else if([[(UIButton *)sender currentTitle]isEqualToString:#"STOP"])
{
[actionButton setTitle:#"START" forState:UIControlStateNormal];
//stop the action here and change the button text to START
}
}
you have at least two options here:
Check for the title of your button and depending on the value call an action or the other.
Create two different buttons, each one with his action and show/hide them.
Related
I have made a custom cell.In each custom cell there are many button like 3 to 4 i want add tap gesture for each button inside that cell.So that i can uniquely identify which cell's button was clicked.I have searched a lot but didn't find any good solution.
Please tell.
you want to access the button you can directly access, no need of Gesture
do like
[yourButton1 addTarget:self action:#selector(selectButtonPressed1:) forControlEvents:UIControlEventTouchUpInside];
yourButton1.tag=indexPath.row;
[yourButton2 addTarget:self action:#selector(selectButtonPressed2:) forControlEvents:UIControlEventTouchUpInside];
yourButton2.tag=indexPath.row;
[yourButton3 addTarget:self action:#selector(selectButtonPressed3:) forControlEvents:UIControlEventTouchUpInside];
yourButton3.tag=indexPath.row;
[yourButton4 addTarget:self action:#selector(selectButtonPressed4:) forControlEvents:UIControlEventTouchUpInside];
yourButton4.tag=indexPath.row;
Method - 1
-(void)selectButtonPressed1:(UIButton*)sender
{
NSLog(#"selcted button1");
}
Method - 2
-(void)selectButtonPressed2:(UIButton*)sender
{
NSLog(#"selcted button2");
}
Method - 3
-(void)selectButtonPressed3:(UIButton*)sender
{
NSLog(#"selcted button3");
}
Method - 4
-(void)selectButtonPressed4:(UIButton*)sender
{
NSLog(#"selcted button4");
}
Lets say you have n number of buttons. And you have one butotn action method:
YOu need to connect this method to all of your n buttons (touch up inside), so that whenever you press a button, this method will get hit.
You can either give tag values for your buttons or you can recognise them by thier title
-(IBAction) nButtonActions:(id)sender{
if([[sender titleLabel] isEqualtoString:#"your button-1 title"]){
//call your method for button -1
}
//or you can do the same using sender.tag
}
use restoration identifier.
button.restorationIdentifier = #"Cell Number _ button tag";
for example
button.restorationIdentifier = [NSString stringWithFormat:#"%#", indexPath.row];
NSString* rowIndexStr = ((UIButton*)sender).restorationIdentifier;
I am working on an app where I have multiple buttons on one page that you click on to go to a different view. The thing is I would like the user to click on them in a specific order. So basically, have all buttons locked except for one and then each time you click on a button that is enabled, it unlocks a new one.
Thanks.
You can do this easily via Tag
When you creating view at that time just put FirstButton in Enabled mode.
OnClick event on every UIButton you can Enabled next one.
For example
i created View with 4 UIButton and given tag 1,2,3,4 at ViewLoad only UIButton with Tag 1 is only Enabled
For all 4 UIButton i have created common Action method as follow which Enabled next UIButton with next Tag
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if([[NSUserDefaults standardUserDefaults] valueForKey:#"LastEnableButton"]){
UIButton * btnTemp = (UIButton *) [self.view viewWithTag:[[[NSUserDefaults standardUserDefaults]valueForKey:#"LastEnableButton"] intValue]];
[btnTemp setEnabled:YES];
NSLog(#"If you want to enable all button which are enabled previously then make for loop up to NSUserDefaults value from starting value");
}
}
- (IBAction)btn_click : (id)sender {
UIButton * btnTemp = (UIButton *) [self.view viewWithTag:([sender tag] + 1)];
if(btnTemp) {
if([btnTemp isKindOfClass:[UIButton class]]) {
[btnTemp setEnabled:YES];
[[NSUserDefaults standardUserDefaults]setValue:[NSNumber numberWithInt:([sender tag] + 1)] forKey:#"LastEnableButton"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
}
I have a UIViewController with a bunch of buttons that each have a (unique) tag. I wrote the following method:
- (void) highlightButtonWithTag: (NSInteger) tag
{
UIButton *btn = (UIButton *)[self.view viewWithTag: tag];
btn.highlighted = YES;
}
What I am trying to do is have a bunch of buttons that each function like a toggle: when I tap one, it should be come active (i.e. highlighted) and the one that was highlighted before should become "un"highlighted.
When the view comes up, I use the viewDidAppear method to set the initial selection:
- (void) viewDidAppear:(BOOL)animated
{
self.selectedIcon = 1;
[self highlightButtonWithTag: self.selectedIcon];
}
And this seems to work just fine: when the view comes up, the first button is selected. However, when I try to update stuff through the #selector connected to the buttons, the previous button is "un"highlighted but the button with sender.tag doesn't get highlighted.
- (IBAction) selectIcon:(UIButton *)sender
{
// "Un"highlight previous button
UIButton *prevButton = (UIButton *)[self.view viewWithTag: self.selectedIcon];
prevButton.highlighted = NO;
// Highlight tapped button:
self.selectedIcon = sender.tag;
[self highlightButtonWithTag: self.selectedIcon];
}
What am I missing here?
The problem is that the system automatically highlights then unhighlights the button on touchDown and touchUp respectively. So, you need to highlight the button again, after it's unhighlighted by the system. You can do by using performSelector:withObject:afterDelay: even with a 0 delay (because the selector is scheduled on the run loop which happens after the system has done it's unhighlighting). To use that method, you have to pass an object (not an integer), so If you modify your code slightly to use NSNumbers, it would look like this,
- (void) highlightButtonWithTag:(NSNumber *) tag {
UIButton *btn = (UIButton *)[self.view viewWithTag:tag.integerValue];
btn.highlighted = YES;
}
- (void) viewDidAppear:(BOOL)animated {
self.selectedIcon = 1;
[self highlightButtonWithTag: #(self.selectedIcon)];
}
- (IBAction) selectIcon:(UIButton *)sender {
// "Un"highlight previous button
UIButton *prevButton = (UIButton *)[self.view viewWithTag: self.selectedIcon];
prevButton.highlighted = NO;
// Highlight tapped button:
self.selectedIcon = sender.tag;
[self performSelector:#selector(highlightButtonWithTag:) withObject:#(self.selectedIcon) afterDelay:0];
}
I have a "Submit" button and a button that toggles between "Edit" and "Cancel." How do I get the "Cancel" button to return to saying "Edit" when I press the "Submit" button? If I press "Cancel," that button will switch back to "Edit."
This is what I have, and it doesn't work.
- (IBAction)submitClicked:(id)sender {
[submitButton resignFirstResponder];
if (inEditMode)
[editButton setClicked:YES];
...
}
- (IBAction)editClicked:(id)sender {
if (inEditMode)
{
inEditMode = NO;
...
[sender setTitle:#"Edit" forState:UIControlStateNormal];
}
else
{
inEditMode = YES;
[sender setTitle:#"Cancel" forState:UIControlStateNormal];
}
[editButton resignFirstResponder];
}
Thanks, John
What you want to do is call the selector - (IBAction) editClicked:(id) sender to
simulate an edit button click when the submit button is clicked. Your code for submit clicked should look like this:
- (IBAction)submitClicked:(id)sender {
[submitButton resignFirstResponder];
if (inEditMode)
[self editClicked:editButton];
...
}
Note that you have to pass in the edit button somehow, so be a little careful, because the method editClicked uses both sender and editButton. You might want to consider consistently using the same variable, as #CodaFi said in a comment.
I have made 20 Buttons dynamically, and I got the tag values of all Buttons.
But I need to know how to use that tag values.
I need information on every button pressed with tag values.
So, how do I use those tag values?
You need to set target-action of each button.
[button setTarget:self action:#selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
Then implement someMethod: like this:
- (void)someMethod:(UIButton *)sender {
if (sender.tag == 1) {
// do action for button with tag 1
} else if (sender.tag == 2) {
// do action for button with tag 2
} // ... and so on
}
Why do you need to use the tag to get the button. You can directly get the buttons reference from its action method.
- (void)onButtonPressed:(UIButton *)button {
// "button" is the button which is pressed
NSLog(#"Pressed Button: %#", button);
// You can still get the tag
int tag = button.tag;
}
I hope you have added the target-action for the button.
[button addTarget:self action:#selector(onButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
You can get reference to that your buttons using that tags. For example, you've added UIButtons to UIView *mainView. To get reference to that buttons you should write following:
UIButton *buttonWithTag1 = (UIButton *)[mainView viewWithTag:1];
Set the tags like this :
for (createButtonIndex=0; createButtonIndex<buttonsCount; createButtonIndex++)
{
buttonCaps.tag=createButtonIndex;
}
And add the method to trap the tags :-
-(void)buttonsAction:(id)sender
{
UIButton *instanceButton = (UIButton*)sender;
switch(instanceButton.tag)
{
case 1(yourTags):
//Code
break;
case 2:
//Code
break;
}
}
Hope this Helps !!
- (IBAction)buttonPressed:(id)sender {
UIButton selectedButton = (UIButton *)sender;
NSLog(#"Selected button tag is %d%", selectedButton.tag);
}
usefully we use btn tag if You Write One Function For (more than one) Buttons .in action if we want to write separate Action For button at that situvation we use btn tag.it can get two ways
I) case sender.tag
//if we have four buttons Add,mul,sub,div having Same selector and add.tag=10
mul.tag=20,sub.tag=30,div.tag=40;
-(IBAction) dosomthing:(id)sender
{
int x=10;
int y=20;
int result;
if(sender.tag==10)
{
result=x+y;
}else if(sender.tag==20)
{
result=x*y;
}else if(sender.tag==30)
{
result=x-y;
}else if(sender.tag==40)
{
result=x/y;
}
NSLog(#"%i",result);
}
2)Case
UIButton *btn=[self.view viewWithTag:10];
then you got object of add button uyou can Hide It With btn.hidden=YES;
UIButton *btn = (UIButton *)[mainView viewWithTag:button.tag];