I'm new to iOS and Xcode. I added some buttons to my iPhone app and now I want to add to them functionality. On Xcode I went to the story board file, right click on a button and then there are story board segues there is costume. I'm clicking the + and nothing happen.
[btn addTarget:self action:#selector(youFunction) forControlEvents:UIControlEventTouchUpInside];
[yourButton addTarget:self action:#selector(yourButtonSelected) forControlEvents:UIControlEventTouchUpInside];
Implement -(void)yourButtonSelected method
-(void)yourButtonSelected{
// Write your code
}
Related
I need to make one button with two actions, touch down used to play a sound and then touch up inside to stop playing the sound when you let go of the button
Connect two methods to both these actions.
[self.yourBtn addTarget:self action:#selector(touchDownMethod:) forControlEvents:UIControlEventTouchDown];
[self.yourBtn addTarget:self action:#selector(touchUpInsideMethod:) forControlEvents:UIControlEventTouchUpInside];
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.
I'm currently designing an app that calls for dynamically creating objects and then displaying them in a ViewController. My problem is wading through the documentation to try and create the objects I need. I've done some searching and even tried to just sit down and figure it out, but alas, I've got nothing to show for it. I already know how to dynamically create the ViewController but I can't seem to get any objects in it.
Does anyone know how I could go about creating objects (Say a button and a slider) dynamically with Objective-C in XCode?
Creating a button in code is simple
- (void)addButton {
// Create button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Set the frame (location and size)
[button setFrame:CGRectMake(0, 0, 40, 30)];
// Set what happens when you touch the button
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
// Set button text
[button.titleLabel setText:#"Button"];
// Add button to view
[self.view addSubview:button];
}
A lot of interface elements follow a similar patter- alloc and init the object, set the frame and add it to a view.
Ok guys I got a problem with UIButton, maybe that´s simple to solve. I have a button with an IBAction toggleMusic which changes the background image.
-(IBAction)toggleMusic{
if (gameAppDelegate.playMusic) {
gameAppDelegate.playMusic = NO;
[menuAudioPlayer stop];
[botaoMusic setBackgroundImage:[UIImage imageNamed:#"bt_sound_off.png"] forState:UIControlStateNormal];
[SoundManager playSoundFile:#"botao3" Format:#"wav" InLoop:NO];
}
else {
gameAppDelegate.playMusic = YES;
[menuAudioPlayer play];
[botaoMusic setBackgroundImage:[UIImage imageNamed:#"bt_sound_on.png"] forState:UIControlStateNormal];
[SoundManager playSoundFile:#"botao3" Format:#"wav" InLoop:NO];
}
}
This is working fine. playmusic (bool) is a stored variable, so everytime I turn the app off and then on, the app already knows if it should play music or not. this is also working fine.
the problem is that I want to set the correct background image (music on/off) as soon as the app is turned on (not only when I click the button). so I put the following code on view will appear:
-(void)viewWillAppear:(BOOL)animated{
if(gameAppDelegate.playMusic)
[botaoMusic setImage:[UIImage imageNamed:#"bt_sound_on.png"] forState:UIControlStateNormal];
else
[botaoMusic setImage:[UIImage imageNamed:#"bt_sound_off.png"] forState:UIControlStateNormal];
}
when I introduce this code, the app succesfully recognizes the correct background image when the view appears, but togglemusic ceases to alter the button background when I touch the button. It may be something silly, but I can´t figure out what I´m doing wrong...
The one thing I notice is in viewWillAppear you call
[botaoMusic setImage:...];
whereas in the button press you call
[botaoMusic setBackgroundImage:...];
It is likely that it is still functioning, just changing the background image BEHIND the top image of whatever was loaded when the view appears
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];