Choosing a button with a variable - objective-c

I have this working in C#, but don't know where to go with Objective C (Xcode specifically).
I have 8 buttons
Stack1, Stack2, etc.
I want to choose Stack(variable) and change the image.
In C# I used
Button Stacks = this.Controls["Stack" + StackNumber.ToString()] as Button;
Stacks.BackgroundImage = .....
Can I do this in Objective C also?

I hope I understand your question correctly. You want to change button's image by clicking on another button. You can assign tag property to your different buttons like stack1.tag = 1001; , stack2.tag = 1002; , stack3.tag = 1003;
Now in your button click method :
- (IBAction)buttonStack1Click:(id)sender // for stack1 button
{
UIButton *tempStack = (UIButton *)[self.view viewWithTag:1003]; //to change image of stack3 button
// Now change the image of stack3 button
[tempStack setImage:[UIImage imageNamed:#"su.png"] forState:UIControlStateNormal];
}
Hope it gives an idea...

Take help of for loop and implement the code below :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
Any query arises,you can tell me.. hope that helps you.

If you want to perform certain function when a button is clicked then there are two ways..
One is Sarah way:Creating Button programmatically
The Other ways is if you are designing buttons in your interface builder :
Then :
1) Make those Buttons an IBOutlet..Also Make IBAction(Lots of tutorials available online..google them)
2) The IBAction will be something like this : - (IBAction)StackButton(YourNumber)Clicked:(id)sender {
In that function change the image of the button you want.
Like
[StackButton1 setImage:MyImage forState: UIControlStateNormal];

Related

iOS UIButton with type UIButtonTypeRoundedRect shows a phantom button?

We have a very simple UIButton with two background images, whenever the button is toggled on there is small rectangle shows on the up left corner of the button. This issue occurs only if the button is UIButtonTypeRoundedRect, if I switch to UIButtonTypeCustom it works fine. Does anybody have a clue?
UIButton *mainBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
mainBtn.tag=0;
mainBtn.frame=CGRectMake(128, 8, 60.0, 60.0);
mainBtn.adjustsImageWhenHighlighted=NO;
[mainBtn setBackgroundImage:[UIImage imageNamed:#"main_tabBar_btn_main.png"] forState:UIControlStateNormal];
[mainBtn setBackgroundImage:[UIImage imageNamed:#"main_tabBar_btn_main_selected.png"] forState:UIControlStateSelected];
[tabBarView addSubview:mainBtn];
[mainBtn addTarget:self action:#selector(tabBarBtnTap:) forControlEvents:UIControlEventTouchUpInside];
try this . . . .
take a custom button , and if you want to get rounded corner's then write following code
#import <QuartzCore/QuartzCore.h>
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0;
btn.layer.cornerRadius = 10;
Is the only reason you want to use UIButtonTypeRoundedRect in order to get the rounded corners (i.e. circle)? If so, try just using UIButtonTypeCustom, and set mainBtn.layer.cornerRadius to the radius you want.

ios change UIButton image at runtime more than one time

I am using storyboard and i want to do a 3 stars priority button, at the first view the stars will appear off (not shining and that is means priority =0), when you first press this button the image of the three not shining stars with change to one star shining, when you press this button again (which is the button with the 3 stars image background) one star will be shining and that means priority = 1 (the image changes), and the press # 2 two stars will be shining, and pressing again make the three stars shining, and in the next press it starts from beginning (none of the stars is shining).
I used this code to implement the button but i do not know how to change the image. any help ?
UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIImage *btnImage = [UIImage imageNamed:#"3star.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[self.view addSubview:btnTwo];
Create a method which will be called when the button is pressed:
[btnTwo addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
and if you don't want to make a property declaration for the button add a tag identifier
btnTwo.tag = 3; //for ex.
in the method you created
-(void) buttonPressed{
UIButton *button = (UIButton*)[self.view viewWithTag:3];
[button setImage:[UIImage imageNamed:#"newImage.png"] forState:UIControlStateNormal];
}
In this method you could then provide logic to assign the correct image based on the number of times pressed.

Label on UIButton of type UIButtonTypeCustom is not displaying

I want to add a borderless UIButton to my view. Using the interface builder, I do this by dragging a Round Rect Button from the object library. Then, on the attributes inspector, change the Type to Custom and leave the title to "Button." Everything is okay with the interface builder. But, this is not the case with the programmatic approach. Programmatically, this is how I do it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
When I run the app, the button is not displayed. Maybe I'm missing something here but when I change the type to UIButtonTypeRoundRect the button is displayed. But, again, I want the button to be borderless.
Anyway, I can always use the interface builder. However, I want to understand why the programmatic approach doesn't work. So, does anyone know what the problem is?
UIButtonTypeCustom is really, actually a custom button type. There are no meaningful values setup for it by default. If you want to display it, you have to set its background color and/or its title color to a non-transparent one:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(x, y, width, height)];
[button setTitle:#"Button" forState:UIControlStateNormal];
// Set visible values
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[someSuperview addSubview:button];
Here i have used your code the Thing you missing is that
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
Hi I was in the same situation.
I do not know why somewhere in my code there was
button.translatesAutoresizingMaskIntoConstraints = NO;
Once I've deleted that line I was able to set my button frame without any issues.
Hope this helps

How to change background image on a button , iOS?

I created a button on my view controller which has a predefined background image. I created an action and an outlet for this button. I want when the user taps the button to change the background image of this button. How can i do that?
I tried to put into the action method of the button something like this:
snapshotCheckbox.image = [UIImage imageNamed:#"snapshot.png"];
but i guess this method is for UImageViews. How can i do the same thing for a button?
Thank you very much for reading my post :D
you can set the image for a given state of the button in the viewDidLoad:
[myButton setBackgroundImage:[UIImage imageNamed:#"myBackgroundImage.png"] forState:UIControlStateHighlighted];
A button has two properties image and backgroundImage..
For setting image use
button.currentImage = image (or)
[button setImage:image ForState:UIControlStateNormal];
For setting backgroundImage use
button.currentBackgroundImage = image (or)
[button setBackgroundImage:image ForState:UIControlStateNormal];
First set the tybe of the button to
button = [UIButton buttonWithType :UIButtonTypeCustom];
then use
[button setImage:[UIImage imageNamed:#"imagename.type"] ForState:UIControlStateNormal];
One solution to do this would be to display the image in a UIImageView and put a transparent UIButton on top of the UIImageView. In Interface Builder you can change a UIButton to "custom".
This would allow you to change the image displayed in the UIImageView easily when handling the action triggered when the UIButton is pushed.
Hope this helps.
You have the setBackgroundImage:forState: method on the button object. See Setting an image for a UIButton in code for more information (seconds answer).
Also, UIButtons automatically change the image when pressed if you set an image for the UIControlStateHighlighted state (though only as long as the user keeps pressing on the button).
Setbutton
if u need image while clicking
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateHighlighted];
or if u want image after Selecting it
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateSelected];
and on that onclick function mention
as
snapshotCheckbox.Selected=YES;
To set an image on a button, just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want! Make sure you have the picture you want in the supporting files to the left.

How to change UIButton's background image while clicking it?

I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.
Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?
Whatever, please explain how to implement this....
The best way is to use multiple images.
You can set different image to different state of a UIButton by using setImage method.
As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.
reference : UIButton States
Hope this helps.