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!
Related
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
}
I have view hierarchy as follows
MyViewController --> ScrollView(added via IB) --> ImageView (+ zooming there) ---> button added on image View(added via code)
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"map.png"]];
[scroll addSubview:imageView];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(660, 120, 40, 40);
[myButton setTitle:#"10" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(asiaPressed:) forControlEvents:UIControlEventTouchUpInside];
[myButton setEnabled:YES];
[imageView addSubview:myButton];
later on in view controller, i defined selector as
-(void) asiaPressed:(id)sender{
NSLog(#"asiaPressed clicked");
}
But it is never going inside selector..
Pls help...i guess it is very basic error..
Thanks in advance
UIImageView has userInteractionEnabled property set to NO by default so it (and its subviews) does not receive touch events. You need to set that property to YES manually:
...
imageView.userInteractionEnabled = YES;
...
P.S. You also mention gesture recognizer in question title, however have not posted any code with it - so if it present it may also cause the problems, but the problem is most likely is what I described above
I have a problem. I've a normal viewcontroller where I add UITextViews in a scrollview. To these UITextviews I add a dynamic number of UIButtons, to which I want to add a target. The reason I add them to the UITextViews is that adding them to the viewcontroller adding the text view's origin will make them end up outside the screen and not scroll, of course. But when I do that, the buttons trigger the action.
My question is: how do I specify the viewcontroller as target? Using self or using the var created in the appdelegate as target does not trigger it. If "two superlevels up from the textview" will work I will use that, just don't know how to specify it correctly.
My code:
UIImage *img=[UIImage imageNamed:#"phonebutton40x30.png"];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:img forState:UIControlStateNormal];
btn.tag=700+i;
btn.frame=CGRectMake(xoffs+3, yoffs+19, 50, 38);
[tvMain addSubview:btn];
Just remove colon from your code
[btn addTarget:self action:#selector(phoneemail:) forControlEvents:UIControlEventTouchUpInside];
The corrected one is
[btn addTarget:self action:#selector(phoneemail) forControlEvents:UIControlEventTouchUpInside];
If I guess, this will probably help you.
Solved it back then by adding the buttons to the scrollview instead of the textview.
I have the following in my view did load on my table view controller:
UIButton *change_view = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[change_view setTitle:#"List" forState:UIControlStateNormal];
[change_view addTarget:self action:#selector(toggleView:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView: change_view];
self.navigationItem.rightBarButtonItem = button;
When I run the program, it doesn't show any title or the rounded rectangle button. However, if I change the type to UIButtonTypeInfoLight, it will show up... so what is the problem?
I'd prefer the way below to set the button's bounds.
[button setTitle:#"Title" forState:UIControlStateNormal];
[button sizeToFit];
Try to set an appropriate frame to your change_view.
When you use UIButtonTypeInfoLight type button uses some built-in size (likely depended on icon used for it), with UIButtonTypeRoundedRect type default frame is applied which must be CGRectZero rect.
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.