How can I make UIButton disable which is created programmatically - ios

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;
}

Related

UIButton UIControl target action 2 actions for single button

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

Recognize click or gesture event on disabled UIButton

My button is disabled but I want to show a message box when user clicks on it. How can I do it? Also I would like to mention that UIbutton is added on UITableview, and I tried the code below, but it always goes to else in DisableClick() function.
In cellForRowAtIndexPath:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 6, 30, 30)];
[button setTag:4000];
[button addTarget:self action:#selector(click:event:) forControlEvents:UIControlEventTouchDown];
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(DisableClick:)];
[self.view addGestureRecognizer:gesture];
[gesture release];
[cell.contentView addSubview:button];
In DisableClick()
- (void)DisableClick:(UIButton*)sender {
UITapGestureRecognizer *recognizer = (UITapGestureRecognizer *)sender;
CGPoint pt = [recognizer locationOfTouch:0 inView:market];
if (CGRectContainsPoint(market.bounds, pt)) {
NSLog(#"Disabled button tapped");
}
else
{
NSLog(#"go out"); // it's always go to here
}
}
Rather than desabling the button keep it enable. Change the image of button like disable image. Put a flag variable when button is tapped use this flag variable to run the code of enabled button and disabled button
BOOL btnFlg;//initialize it true or false on cellForRowAtIndex... method
- (void)DisableClick:(UIButton*)sender
{
if(btnFlg)
{
btnFlag=NO;
NSLog("button disabled");
}
else
{
btnFlag=YES;
NSLog("button enabled");
}
}
Why you are initialize a tap gesure for this button. You set the selector in ur addTarget
Maintain a btn boolean for the button status . Change the status whenever you want to disable or enable.
BOOL btnEnabled;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 6, 30, 30)];
[button setTag:4000];
[button addTarget:self action:#selector(DisableClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
Make the target function as
- (void)DisableClick:(UIButton*)sender {
//set the function that you needed when the user tap on this button here.
if(btnEnabled) {
// here invoke the functions you needed when the button enabled
} else {
UIAlertView *msg = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Your button is disabled" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[msg show];
}
}
You will need to subclass UIButton and implement
touchesBegan:withEvent: and touchesEnded:withEvent: methods for your purpose.

button of a dynamically created view does not respond

I need your help. I am trying to build a game. When a hint button is pressed a new view is created programmatically with the code below:
In my gameController.m
//connect the Hint button
-(void)setHud:(HUDView *)hud
{
_hud = hud;
[hud.btnHelp addTarget:self action:#selector(actionHint) forControlEvents:UIControlEventTouchUpInside];
}
//the user pressed the hint button
-(void)actionHint
{
CGRect viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
[self.gameView addSubview:hintMenu];
}
This is my new View created programmatically.
+(instancetype)viewWithRect:(CGRect)r
{
HintMenu* hint = [[HintMenu alloc] initWithFrame:r];
hint.userInteractionEnabled = YES;
UIImage* image=[UIImage imageNamed:#"btn"];
hint.btnHelp = [UIButton buttonWithType:UIButtonTypeCustom];
[hint.btnHelp setTitle:#"button" forState:UIControlStateNormal];
hint.btnHelp.titleLabel.font = kFontHUD;
[hint.btnHelp setBackgroundImage:image forState:UIControlStateNormal];
hint.btnHelp.frame = CGRectMake(50, 30, image.size.width, image.size.height);
hint.btnHelp.alpha = 0.8;
[hint addSubview: hint.btnHelp];
UIImage* image2=[UIImage imageNamed:#"tile"];
hint.imageHelp=[[UIImageView alloc] initWithFrame:CGRectMake(image.size.width + 50 + 10, 30, image2.size.width, image2.size.height/2)];
hint.imageHelp.image=image2;
[hint addSubview:hint.imageHelp];
return hint;
}
I want to identify when the button in the new view is being pressed. Similarly with the above I add in my GameController.m
//connect the button in the Menu
-(void)setHintMenu:(HintMenu *)hintMenu
{
_hintMenu = hintMenu;
[hintMenu.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
}
//the user pressed the menu button
-(void)actionHintMenu
{
NSLog(#"Button in menu pressed");
}
but the log is not shown. Can you help me on this?
edit:
If this code is added in the HintMenu.m which is my new Created view like this:
+(instancetype)viewWithRect:(CGRect)r
{
...
hint.btnHelp.alpha = 0.8;
[hint.btnHelp addTarget:self action:#selector(actionHintMenu:) forControlEvents:UIControlEventTouchUpInside];
[hint addSubview: hint.btnHelp];
..
}
//the user pressed the menu button
-(void)actionHintMenu:(UIButton *) button
{
NSLog(#"Button in menu pressed");
}
I am getting this error:
2013-06-16 16:44:30.559 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[HintMenu actionHintMenu]: unrecognized selector sent to class 0xe6f8'
*** First throw call stack:
Answer to you edited question
Replace
[hint.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
with
[hint.btnHelp addTarget:hint action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
I am not sure why you have added [hintMenu.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside]; inside the setter method of your hintMenu view.
There is no call for the setter method of hintMenu and so the button target-action is not getting set.
I would suggest remove the -(void)setHintMenu:(HintMenu *)hintMenu completely.
Update actionHint
-(void)actionHint
{
CGRect viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight,kMenuWidth*10, kMenuHeight*2);
HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
[hintMenu.btnHelp addTarget:self action:#selector(actionHintMenu) forControlEvents:UIControlEventTouchUpInside];
[self.gameView addSubview:hintMenu];
}
This is just a fix. If you ask me the right method, I would suggest setting the button target should be done within your custom view itself. For that you might maintain a delegate property in the view.

How to make UIButton dragable but don't trigger click event

I usually use this method to implement the UIButton movable;
[button addTarget:self action:#selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];
But it will trigger the touchUpInside event at same time, and I need touchUpInside event to do some other things.
So does anyone know how to avoid this?
Thanks a lot;
use these code below to solve it;
[button addTarget:self action:#selector(touchDown:withEvent:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(touchDragInside:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(touchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];
- (void) touchUpInside :(UIControl*)c withEvent:ev{
NSLog(#"touch Up inside");
if (count) {
NSLog(#"here I can do something about clicking event");
}
}
- (void) touchDown:(UIControl*)c withEvent:ev{
NSLog(#"touch Down");
count = YES;
}
-(void) touchDragInside:(UIControl*)c withEvent:ev{
NSLog(#"touch drag inside");
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
count = NO;
}
Actually the best way to accomplish this is to use UIPanGestureRecognizer and add it to the button. It handles the dragging very well and you also can tap on it without needing to do anything else.

How to find which button is pressed on the array of buttons in ios

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

Resources