i am fresh in iphone..i have created one button and through coding and want to call a method by that button what should i do please help me..
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 150, 44); // position in the parent view and set the size of the button
[myButton setTitle:#"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[myview addSubview:myButton];
The answer is in the question itself
// add targets and actions
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
this line added a action to the button which is in self with action name buttonClicked: for the controlevent touchUpInside
so when a button is touchupInside it will execute the method buttonClicked
Therefore
- (void)buttonClicked: (UIButton *)sender
{
// do whatever you want to do here on button click event
}
will get executed on button action
- (void)buttonClicked:(id)sender
{
// your code
}
- (void)buttonClicked: (UIButton *)sender
{
// do stuff
}
Take a look at UIControl documentation
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
action : A selector identifying an action message. It cannot be NULL.
You are passing #selector(buttonClicked:) as action. #selector() is a compiler directive to turn whatever's inside the parenthesis into a SEL. A SEL is a type to indicate a method name, but not the method implementation.[1] so implement -(void)buttonClicked:(id)sender in your class.
- (void)buttonClicked: (UIButton *)sender
{
// do whatever you want to do here on button click event
}
Related
I have a method with multiple variables:
-(void) showImg:(UIBarButtonItem *)sender string1:(NSString *) string2:(NSString *);
I want to send NSString values (As this function is used for multiple elements).
This is how I add my action when no parameters are needed:
[myButton addTarget:self action:#selector(showImg) forControlEvents: UIControlEventTouchUpInside];
I tried adding parameters within the #selector like this:
[myButton performSelector #selector(showImg:string1:string2::) withObject:#"-1" withObject:#"-1"];
But this does not work. How may I call my function with multiple parameters directly inside the #selector?
You can make your UIButton call a function in between (like a middle man), to control the next functions parameters.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonTapped:(id)sender{
if(<some logical condition>){
[self showImg:sender string1:#"-1" string2:#"-1"];
}else {
[self showImg:sender string1:#"otherVal1" string2:#"otherVal2"];
}
}
-(void) showImg:(id)sender string1:(NSString *)string1 string2:(NSString*)string2 {
//Other logic
}
The following line is wrong :
[myButton performSelector #selector(showImg:string1:string2::) withObject:#"-1" withObject:#"-1"];
You can't pass parameters like this to the selector, that's why you have an error. I don't think you can pass multiple parameters to a selector. Maybe you can try to set a tag to your button with your const value (works with integers)
For example :
//Init your button......
[myButton setTag:1];
[myButton addTarget:self action:#selector(showImg:) forControlEvents: UIControlEventTouchUpInside];
- (void)showImg:(id)sender {
UIButton *btn = (UIButton *)sender;
int value = btn.tag;
}
Just a suggestion.. :)
Say for example i have two volume buttons ( + and - )
How can I implement something such as when holding the + button, it will raise up the volume incrementally at an interval? (I'm only interested in doing an action at an interval while the button is being pressed)
You can use a timer for this. Start the timer when the touch starts. If the timer expires, increase or decrease the volume and restart the timer. When the touch ends, cancel the timer.
Assuming you have two button one for - and other for + ,
you could store the interval information in the tag field of your button
Installing the interval value in your button tag property.
myPulseButton.tag = 10;
myMinusButton.tag = 10;
Adding action with your button.
[myPulseButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[myMinusButton addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
Implement the buttonEvent method like below.
-(void) buttonEvent:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton == myPulseButton)
{
[self increaseVolume:myPulseButton.tag];
}
else if(myButton == myMinusButton)
{
[self decreaseVolume:myMinusButton.tag];
}
}
First add the button...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(increase)forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
then write the method for buttton press.
-(void)increase
{
//increase the volume here
}
if it is not the solution you want means add the comment...
I'm using TTCatalog and creating a TTButton using the following:
TTButton *button = [TTButton buttonWithStyle:#"toolbarButton:" title:#"click button"];
I tried adding a target and an action to that, but that doesn't work. How do I use it?
You are missing the "[".
TTButton *button = [TTButton buttonWithStyle:#"toolbarButton:" title:#"click button"];
[button setUserInteractionEnabled:YES];
[button addTarget:self action:#selector(buttonPressed:event:) forControlEvents:UIControlEventTouchUpInside];
Then just set button properties, use target/action to set an event handler. (The TTButton is just a UIEvent subclass.)
Good luck!
I'm currently dynamically creating UIButton objects in my view.
I have an NSMutableArray containing information about them (id - label).
I then create my view objects by doing a for iteration on my MutableArray.
I'm trying to use this code on my buttons to catch touche events :
[myButton addTarget:self action:#selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];
My selectedButton method is called with success, but I don't have any idea on knowing with button were touched.
I tried to do this :
-(void)selectedButton:(id)sender {...}
But don't know what to do with the sender object.
Thanks in advance for your help !
At the top of your .m file, put something like this:
enum {
kButtonOne,
kButtonTwo
};
When you're creating your buttons, do this
myButton.tag = kButtonOne;
Then in your selected button method, do this:
-(void)selectedButton:(id)sender {
switch (sender.tag) {
case kButtonOne:
// do something here
break;
case kButtonTwo:
// do something else here
break;
}
}
Set mybutton.tag to something, and then check for that tag in selectedButton:sender.
-(void)viewDidLoad{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(80.0, 210.0, 40.0, 30.0);
button.tag=1;
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"raaz" forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(IBAction)aMethod:(id)sender{
UIButton *btn=(UIButton *)sender;
NSLog(#"I have currently Pressed button=%d",btn.tag);
}
I am trying to make a standard check box for my iPhone app from a UIButton with a title and image. The button image changes between an "unchecked" image and a "checked" image.
At first I tried subclassing UIButton but UIButton has no -init*** method to use in my -init method.
What is the best way to do this?
Thanks in advance.
You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.
UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.
You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:
// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:checkedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}
Set the images in the button:
[button setImage:uncheckedImage forState:UIControlStateNormal]
[button setImage:checkedImage forState:UIControlStateSelected]
Now all you need to do is:
button.selected = state
and the correct images will display.
All you need to do is set 2 different images for the states UIControlStateNormal and UIControlStateSelected, then in your selector, changed the selected property of the button.
Here is a working example (replace image names with your own):
- (void)loadView {
// ...
UIButton *chkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[chkBtn setFrame:CGRectMake(0, 0, 300, 25)];
[chkBtn setImage:[UIImage imageNamed:#"UNCHECKED.png"]
forState:UIControlStateNormal];
[chkBtn setImage:[UIImage imageNamed:#"CHECKED.png"]
forState:UIControlStateSelected];
[chkBtn addTarget:self
action:#selector(chkBtnHandler:)
forControlEvents:UIControlEventTouchUpInside];
// Optional title change for checked/unchecked
[chkBtn setTitle:#"I am NOT checked!"
forState:UIControlStateNormal];
[chkBtn setTitle:#"I am checked!"
forState:UIControlStateSelected];
[self.view addSubview:chkBtn];
[chkBtn release], chkBtn = nil;
// ...
}
- (void)chkBtnHandler:(UIButton *)sender {
// If checked, uncheck and visa versa
[sender setSelected:!sender isSelected];
}
For anyone interested in the future - instead of doing it yourself just download the link below from GitHub and it has it subclassed from UIControl already and functions perfectly as a checkbox. Also includes a sample project on how easy it is to use:
https://github.com/Brayden/UICheckbox
I have used M13Checkbox in one of my projects. Works ok.
https://github.com/Marxon13/M13Checkbox
Did you try overriding the initWithCoder method, just in case it is loaded from a nib somehow?
UIImage* nonCheckedImage=[UIImage imageNamed:#"ic_check_box_outline_blank_grey600_48dp.png"];//[UIImage init
UIImage* CheckedImage=[UIImage imageNamed:#"ic_check_box_black_48dp.png"];//[UIImage init
//ic_check_box_black_48dp.png
[_checkBox setImage:CheckedImage forState:UIControlStateSelected];
[_checkBox setImage:nonCheckedImage forState:UIControlStateNormal];
}
- (IBAction)checkBoxToggle:(id)sender {
_checkBox.selected = !_checkBox.selected; // toggle the selected property, just a simple BOOL
}
the image you can use google icon
Try this:-
-(IBAction)clickCheckButton:(UIButton *)sender {
if (sender.tag==0) {
sender.tag = 1;
[sender setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}else
{
sender.tag = 0;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal]; } } sender.tag = 0;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
}
Why not use a switch - UISwitch? This is used to display an element showing the boolean state of a value.