I have a button that has a selected and non-selected state.
My target action code for the button is this:
NSArray *targets = [myButton actionsForTarget:self forControlEvent:UIControlEventTouchUpInside];
if ([targets count] == 0) {
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
When the button is pressed, it goes to selected state in the method.
If the button is selected, a different action should be called. Is there any unintended consequence to doing it this way or is there a way to add actions with UIControlState instead?
if (myButton.selected == NO){
[myButton addTarget:self action:#selector(save:) forControlEvents:UIControlEventTouchUpInside];
}
else{
[myButton addTarget:self action:#selector(delete:) forControlEvent:UIControlEventTouchUpInside];
}
-(IBAction)myButton:(id)sender
{
UIButton *btn = (UIButton*)sender;
btn.selected = !btn.selected;
if(btn.selected)
{
//Do whatever you want when button is selected
[self delete];
}
else
{
//Do whatever you want when button isDeselected
[self save];
}
}
Your approach might be confusing. You can use like this.
[myButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
- (void)buttonPressed:(UIButton *)sender
{
if(sender.selected)
[self save];
else
[self delete];
}
-(void)save
{
// your code
}
-(void)delete
{
// your code
}
i believe that you are wanting something like toggle button. if i am right use yourbutton.currentTitle to follow the functionality. i.e. if [youtbutton.currentTitle isEqualToString: #"save]" then you should perform save and set that to delete and vice verse
Related
if( [[favbutton imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"wishlwhite.png"]])
{
[favbutton setImage:[UIImage imageNamed:#"wishlistwhite.png"] forState:UIControlStateNormal];
// other statements
}
else
{
[favbutton setImage:[UIImage imageNamed:#"wishlwhite.png"] forState:UIControlStateNormal];
// other statements
}
You really shouldn't be checking the image in the button to determine state. Declare a separate property to test, e.g.
#property (nonatomic, assign) BOOL isWishList; // or something
-(IBAction) toggleButton: (id) sender {
if (self.isWishList) {
[favbutton setImage:[UIImage imageNamed:#"wishlistwhite.png"] forState:UIControlStateNormal];
// other statements
} else {
[favbutton setImage:[UIImage imageNamed:#"wishlwhite.png"] forState:UIControlStateNormal];
// other statements
}
self.isWishList = !self.isWishList
}
I am adding an image in IBAction, I want to restore the old image back once the button is tapped.
It is not the image in the UIButton, it's the image in the UIImageView
- (IBAction)findMe:(id)sender
{
[self.imageView setImage:[UIImage imageNamed:#"image2"]];
}
I want to restore#"image1". immediate once button is tapped
some thing i need is some thing similar to
button setImage:[UIImage imageNamed:#"clicked.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
but i don't want to change Button Image, but change UIImageView Image when clicked
You can use the following code
[yourButton addTarget:self action:#selector(touch2:) forControlEvents:UIControlEventTouchDown];
[yourButton addTarget:self action:#selector(touch3:) forControlEvents:UIControlEventTouchUpInside];
Now the selectors
-(void)touch2:(UIButton *)send
{
imageView.image= [UIImage imageNamed:#"image1"];
}
-(void)touch3:(UIButton *)send
{
imageView.image= [UIImage imageNamed:#"image2"];
}
Try it out:
- (IBAction)findMe:(id)sender
{
if (self.imageView.image) {
// store your old image here
NSLog(#"%#", self.imageView.image)
}
[self.imageView setImage:[UIImage imageNamed:#"image2"]];
}
Try this first create gesture for UIButton
UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleBtnLongPressgesture:)];
[buttonFindMe addGestureRecognizer:btn_LongPress_gesture];
When you press and release the button it calls this method.
-(void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{
//as you hold the button this would fire
if (recognizer.state == UIGestureRecognizerStateBegan) {
//Change your image 1
}
//as you release the button this would fire
if (recognizer.state == UIGestureRecognizerStateEnded) {
//Change your image 2
}
}
I created some buttons in my code. I want to set it disable but I'm not sure how. here's my code.
UIButton *btn_levels = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_levels.frame=CGRectMake(x-10, y, 40, 40);
[btn_levels setTitle:[beginer_lvl objectAtIndex:i] forState:UIControlStateNormal];
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
btn_levels.tag =i;
btn_levels.backgroundColor=[UIColor blackColor];
btn_levels.tintColor=[UIColor cyanColor];
NSLog(#"btn nm=%#",[beginer_lvl objectAtIndex:i]);
[self.scroll addSubview:btn_levels];
and that is the button method but I don't know what to do...
-(void)btn_Method
{
//to make button disable
}
Modify this line
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
and this method
-(void)btn_Method:(UIButton*)sender
{
sender.enabled = NO;
}
change the statement
[btn_levels addTarget:self action:#selector(btn_Method) forControlEvents:UIControlEventTouchUpInside];
to
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
Also change the below method -
-(void)btn_Method
{
//to make button disable
}
to
-(void)btn_Method:(UIButton*)button
{
//to make button disable
[button setEnabled:NO];
}
disabled but visible
btn_levels.enabled = NO;
also invisible
btn_levels.hidden = YES;
Target actions can send the object for which it'll performed the action. To make your method to know which button was tap, you need to make its definition to get the button inside the method. So just change you button definition to look like this,
- (void) btn_Method:(UIButton *)sender {...}
also, where you're adding target to the button you've to add a colon ( : ) after the method name to tell the compiler that, this action will need an object on which an action was added. So that line should look like this,
[btn_levels addTarget:self action:#selector(btn_Method:) forControlEvents:UIControlEventTouchUpInside];
^
Here, we will get a sender (you can give any name to your argument) of type UIButton with btn_Method action call. That's it, now you can do anything with that button, so in your case you want to make it disable, so the method would look like this,
- (void) btn_Method:(UIButton *)sender {
sender.enabled = NO;
}
I created five buttons using for loop, it works well. Whenever i click restart button duplicate buttons created
-(void)buttonCreate {
for( int i = 0; i < 5; i++ )
{
oneBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn1.frame = CGRectMake(316,i*-5+474,51,50);
[oneBtn1 setTag:i];
[oneBtn1 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchUpInside];
[oneBtn1 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn1];
}
}
Restart button function:
-(void)restart {
[self buttonCreate];
}
I tried this one but it will remove only one button out of 5.
if(oneBtn1 != NULL) {
[oneBtn1 removeFromSuperview];
oneBtn1 = nil;
}
Problem is: How to remove duplicate buttons?
Create an array(NSMutableArray *buttonHolderArray) to hold outlets for all five buttons.
[buttonHolderArray addObject:oneBtn1];
Then you can remove/update the buttons as and when you need.
And once you dont want any button, empty the array itself. On top of this, if you wish to clear the view then simply call removeFromSuperView for all the buttons
I tried this one but it will remove only one button out of 5.
if(oneBtn1 != NULL) {
[oneBtn1 removeFromSuperview];
oneBtn1 = nil;
}
You are removing only one button, if you want to remove all then use
for ( UIButton *button in buttonHolderArray){
button removeFromSuperView];
}
You can remove by checking if the button is already created.
-(void)buttonCreate {
int tag_start = 500;
for( int i = 0; i < 5; i++ )
{
UIView * prevbtn = [self.view viewWithTag:tag_start + i];
if(prevbtn) {
[prevbtn removeFromSuperview];
}
oneBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn1.frame = CGRectMake(316,i*-5+474,51,50);
[oneBtn1 setTag:tag_start + i];
[oneBtn1 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchUpInside];
[oneBtn1 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn1];
}
}
use this before calling again restart method
for (UIButton *btn in self.view.subviews ) {
[btn removeFromSuperview];
}
Before the restart button clicked you can remove all the buttons from you view calling the method removeChildViewsWithKindOfClass
-(void)restart {
[self.view removeChildViewsWithKindOfClass:[UIButton class]];
[self buttonCreate];
}
-(void)removeChildViewsWithKindOfClass:(Class)classToRemove{
if (classToRemove==nil) return;
for (UIView *v in [self subviews]) {
if ([v isKindOfClass:classToRemove]) {
[v removeFromSuperview];
}
}
}
If you want to remove a specific button you can use the property tag that you have to set beforehand when you create your button and it allows you removing the button with a specific tag.
I have an array of buttons,I want the text on each button..to be displayed on the label..so friends,please tell me how can I achieve this
Regards
Ranjit
Simply add button in this way
Button.tag = i
[Button addTarget:self action:#selector(MenuDetail:) forControlEvents:UIControlEventTouchUpInside];
[listOfButtons addObject:Button];
and handle touch up event as following
-(IBAction)MenuDetail:(id)sender
{
UIButton *temp= (UIButton *)sender;
NSInteger parentValue=temp.tag;
clsMenuItem *tempMenu=[appDelegate searchMenu:parentValue];
if(parentValue==1)
{
// do something
}
else if(parentValue==1)
{
// do something
}
}
hope this helps