Changing button text of different button on button click - ios

I have 3 buttons in my storyboard and ViewController that are working as expected:
- (IBAction)button0:(id)sender {
[sender setTitle:#"btn0 pressed" forState:UIControlStateNormal];
}
- (IBAction)button1:(id)sender {
[sender setTitle:#"btn1 pressed" forState:UIControlStateNormal];
}
- (IBAction)button2:(id)sender {
[sender setTitle:#"btn2 pressed" forState:UIControlStateNormal];
}
I have a fourth button that, when pressed, I would like to change the displayed text of button0-2 to an empty string.
- (IBAction)resetAllButtons:(id)sender {
//In Android this code would be something like:
//Button btn0 = (Button) findViewById(R.id.button0);
//btn0.setText(" ");
}
How do I do this? I've found many ways to change the button text, but only of the current button being pressed. Can I target all the buttons by id somehow?

Figured it out (although still not clear on why it works?)
I connected button0 as an IBOutlet in my ViewController.h file.
#property (weak, nonatomic) IBOutlet UIButton *button0;
From there I was able to reference it in my ViewController.m file using
[self.button0 setTitle:#" " forState:UIControlStateNormal];
But why am I able to do that? I thought that if I declared
- (IBAction)button0:(id)sender;
in my ViewControler.h file that I couldn't also have an outlet connected to the same object? Thanks for reading either way.

Related

Alternative option of UIButton addTarget:action:forControlEvents: method in IOS

I know that this question has been asked so many times but i want to pass a custom object as an argument on clicking a button.
UIButton addTarget:action:forControlEvents: doesn't allow us to do that but it is important for me so I can do further on the basis of custom objects.
If an alternative for add target is possible, so please give the solution.
The code is like this:
Custom Object:
HeaderData *cell ;
Button:
_forward =[UIButton buttonWithType:UIButtonTypeRoundedRect];
[_forward setTitle:#"F" forState:UIControlStateNormal];
_forward.frame = CGRectMake(300, (_CELL_HEIGHT-_LABEL_HEIGHT)/2, 10 ,_LABEL_HEIGHT);]
[_forward addTarget:web action:#selector(functionName:) forControlEvents:UIControlEventTouchUpInside];
and the function which is called on clicking the UIButton _forward is:
-(void)functionName:(UIButton *)sender{
//code for further programming on the basis of cell.
}
You could make a custom subclass of UIButton:
#interface CustomDataButton : UIButton
#property (nonatomic, strong) id userData;
#end
Then in functionName, you can pull the data back out:
-(void)functionName:(UIButton *)sender
{
if ([sender isKindOfClass:[CustomDataButton class]])
{
id customData = ((CustomDataButton *) sender).userData;
}
}
caveat: written without an IDE. Watch out for typos etc.

UIButton selected state change image

I know this should be very simple but since I really have looked around here and on google without getting my code to work I will ask. Yes, I have checked related questions and they does not work...
I simply want to change the image of the button when it is pressed.
This is my header file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIButton *myButton;
#end
And my implementation file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.myButton setImage:[UIImage imageNamed:#"normal.png"] forState:UIControlStateNormal];
[self.myButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
}
#end
The button is built and connected in Main.storyBoard and I have also tried to do it in the attributes inspector but neither way worked!
Please note that I am not looking for how to do highlighted state but selected.
I believe what you need to do is set the button's state to selected when the button is tapped. Probably something like this:
-(IBAction)buttonTapped:(id)sender {
if(self.myButton.selected)
[self.mybytton setSelected:NO];
else
[self.mybutton setSelected:YES];
}
You need to set the image for each state from your interface builder and you don't need to add a code if you want :
Select the State Config to Default and then choose an image
Select the State Config to Selected and then choose an image
And now to test each state of the button:
You can do the same for background image just select the parameter "Background"
Change to whatever image you need in the IB builder in the State Config and add an action to that button and in that add this line of code as said by #Aladin.
#IBAction func btnPressed (_ sender: UIButton) {
// If you want to change it for selected state when tapped.
sender.isSelected = !sender.isSelected
}
Do you have a method for when the button is clicked? If yes, put this below lines in there:
[self.myButton setSelected:YES];
[self.myButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
Why not try to set background image default to "normal.png";
[self.mybutton setBackgroundImage:[UIImage imageNamed:#"normal.png"]];
And switch to "selected.png" when perform the button.
[self.mybutton setBackgroundImage:[UIImage imageNamed:#"selected.png"]];

objective-c how to modify button text of a button different than the button pressed

This is what I would use to change the text if it were the same method but I want to change it from a different method.
-(IBAction)StartTimer:(id)sender {
[sender setTitle:#"Stop" forState:UIControlStateNormal];
}
-(IBAction)ResetAllData:(id)sender {
[(NEED THIS PIECE) setTitle:#"Start" forState:UIControlStateNormal];
}
There are several ways of doing it. Perhaps the simplest one is to make an IBOutlet for the other button, give it a name (say, anotherButton), attach it in the storyboard, and call it directly, like this:
-(IBAction)ResetAllData:(id)sender {
[anotherButton setTitle:#"Start" forState:UIControlStateNormal];
}
An alternative would be to add a tag * to the button in question (say, 123), and then reference it by its numeric tag, like this:
-(IBAction)ResetAllData:(id)sender {
[[self viewWithTag:123] setTitle:#"Start" forState:UIControlStateNormal];
}
* To add a tag, select the button in the interface builder, open its properties, find the tag, and type in the desired value in the text field.
Make the UIButton you want to change the title of a property of the class, then you will have direct access to it. For example, the following could be put in your ViewController's .m file.
#interface ViewController ()
#property (nonatomic) UIButton *otherButton;
#end
Then in your first IBAction, just set the title of otherButton.
-(IBAction)ResetAllData:(id)sender {
[self.otherButton setTitle:#"Start" forState:UIControlStateNormal];
}

iOS: setting buttons pressed state

I am trying to find the best approach to doing this. I have 5 custom buttons on a view controller and I am trying to have the button stay highlighted if it is clicked. I know how to do this but I am trying to only allow 1 button to be highlighted at a time. So if a user clicks a button and highlights it, but clicks another, then the most recent button clicked will stay highlighted and the previous will unhighlight. What would be the best way to accomplish this?
You should keep a reference to all your buttons (for example, if you use IB, have links in your code like #property (nonatomic, strong) IBOutlet UIButton *button1; for all your buttons).
Then link all your buttons to the same method for a press on the button. I'll call it buttonPressed.
Impement it like this :
- (IBAction)buttonPressed:(id)sender {
UIButton *buttonPressed = (UIButton*)sender;
NSArray *buttons = [NSArray arrayWithObjects:_button1, _button2, _button3, nil];
bool buttonIsHighlighted = NO;
// Check if a button is already highlighted
for (UIButton *button in buttons) {
if (button.highlighted) {
buttonIsHighlighted = YES;
}
}
// If a button is highlighted, un-highlight all except the one pressed
// If no button is highlighted, just highlight the right one
if (buttonIsHighlighted) {
for (UIButton *button in buttons) {
if (buttonPressed == button) {
buttonIsHighlighted = YES;
} else {
button.highlighted = NO;
}
}
} else {
buttonPressed.highlighted = YES;
}
}
I can't test this code but I'm pretty sure it should work. Let me know if something's wrong.
Solution 1:
Put your buttons in an NSArray and when user clicks on a button check if another is highlighted. If YES, unhighlight it and highlight the one was pressed. If NO, highlight directly the one pressed.
Solution 2:
You can save the highlighted button in a global variable declared in #interface or in a #property. When users click the new one unhighlight the previous.

UIbutton which displays a popover label briefly where the label would show halfway outside the button

How do I create a uibutton, which will show a uilabel of some text briefly something like a speech bubble that will appear and then disappear. The problem is the speech bubble will be a view lying halfway outside the button frame.
thanks for help on this.
Add a UIButton in the .xib file and give some name.
Add the folowing method in your code.
- (IBAction)bubbleButn:(UIButton *)btn
Then right click on the button and see the "Touch Up Inside" part in the menu. Then you can click and drag the right most circle to the files owner in the "Placeholders" section of the Xocde. Then you can see the method name comes in a menu. You can select our method from there.
Next we are going to implement our code.
At first you must declare a UILabel in the .h file.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
UILabel *bubbleLabel_;
}
#end
and then in the .m file implement the button action and label settings.
- (IBAction)bubbleButn:(UIButton *)btn {
float half = btn.frame.size.width/2;
float yVal = btn.frame.size.height/2;
if(bubbleLabel_) {
/*
If you are using 'arc' no need of this statement
add this in the dealloc method also.
*/
[bubbleLabel_ release];
bubbleLabel_ = nil;
}
bubbleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(btn.frame.origin.x+half, btn.frame.origin.y+yVal, 40, 50)];
bubbleLabel_.text = #"Hi";
[self.view addSubview:bubbleLabel_];
[self performSelector:#selector(hideBubble) withObject:self afterDelay:3];
}
- (void) hideBubble {
[bubbleLabel_ removeFromSuperview];
}
The label named "Hi" will be there for 3 seconds.
Go on...

Resources