Remove the old button when double tap - iphone

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"restaurant.png"] forState:UIControlStateNormal];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"restaurant1.png"] forState:UIControlStateNormal];
if([scaleStr isEqualToString:#"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:#"0.209607"]) {
[btn1 removeFromSuperView];
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
i have set the button when doubletap the background image in UIScrollview.If i double tap second time , the first button also show.I need to remove the button which i created first .
Also i remove the btn1 in next condition.
Kindly help me regarding on this.

A little bit of context might help here, but my guess is that you're really just creating a new button each time and not holding a reference to the old one. So when you call removeFromSuperView on the second tap, you're removing a button that hasn't even been added to the view, not the one you added on your last pass. Try making your buttons instance variables instead of local. In extremely loose pseudo-code:
#interface MyClass : UIView {
UIButton *btn1;
UIButton *btn2;
}
#implementation MyClass {
-(MyClass *) init {
self = [super init];
[self setupButtons];
return self;
}
-(void) setupButtons {
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setImage:[UIImage imageNamed:#"restaurant.png"] forState:UIControlStateNormal];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn2 setImage:[UIImage imageNamed:#"restaurant1.png"] forState:UIControlStateNormal];
}
-(void) myFunction {
[btn1 removeFromSuperView];
[btn2 removeFromSuperView];
if([scaleStr isEqualToString:#"0.139738"]){
btn1.frame = CGRectMake(330,145,5,5);
[imageScrollView addSubview:btn1];
} else if ([scaleStr isEqualToString:#"0.209607"]) {
btn2.frame = CGRectMake(495,217.5,10,10);
[imageScrollView addSubview:btn2];
}
}
}

Related

How to disable UIButton highlight action

I'm trying to implement an analog of segmented control with two buttons. In default state they have no images, only labels, in selected one they have background image. I want to activate control with TouchDown event.
here is the code (I removed all unnecessary things):
-(IBAction) onButton1
{
button1.selected = YES;
button2.selected = NO;
}
-(IBAction) onButton2
{
button1.selected = NO;
button2.selected = YES;
}
the problem is: assume button1 is selected. When I touch button2 it does not change its image to "selected" image (there is no default image, as I've said), but when I release finger it changes. Also, if I touch already selected button it removes "selected" image and return it when I release it.
I've set highlighted state of buttons, so they have "selected" image in that state, but it did not help (not only in IB, but also with [button setBackgroundImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateHighlighted];). I've set adjustsImageWhenHighlighted = NO, that did not help too, again, in both program way and IB.
I've seen a lot of similar (but not identical) questions here, but they did not work for me.
Thanks in advance
Since, you wanted to activate the action at touch down event, I assume that you dont need highlighting. Try the following code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(10.0f, 10.0f, 50.0f, 50.0f);
[button1 setBackgroundImage:[UIImage imageNamed:#"btn.jpeg"] forState:UIControlStateNormal];
[button1 setBackgroundImage:[UIImage imageNamed:#"btnsel.jpeg"] forState:UIControlStateSelected];
[button1 addTarget:self action:#selector(button1Action:) forControlEvents:UIControlEventTouchDown];
[button1 addTarget:self action:#selector(button1TouchUp:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[self.view addSubview:button1];
button1.selected = YES;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(80.0f, 10.0f, 50.0f, 50.0f);
[button2 setBackgroundImage:[UIImage imageNamed:#"btn.jpeg"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:#"btnsel.jpeg"] forState:UIControlStateSelected];
[button2 addTarget:self action:#selector(button2Action:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:#selector(button2TouchUp:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[self.view addSubview:button2];
}
- (void)button1Action:(UIButton*)sender
{
button1.highlighted = NO;
button1.selected = YES;
button2.selected = NO;
}
- (void)button2Action:(UIButton*)sender
{
button2.highlighted = NO;
button2.selected = YES;
button1.selected = NO;
}
- (void)button1TouchUp:(id)sender
{
button1.highlighted = NO;
}
- (void)button2TouchUp:(id)sender
{
button2.highlighted = NO;
}

enabled a programmatically created button in Xcode

I know how to set the enable = YES/NO, a button created using a property and xib. However, how do you do the same thing to a programmatically created button from another method in the same class?
For example here is my button in the viewDidLoad:
UIButton *AllList = [UIButton buttonWithType:UIButtonTypeCustom];
AllList.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:#"allModsBtn.png"];
[AllList setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:AllList];
// add targets and actions
[AllList addTarget:self action:#selector(getButtons:) forControlEvents:UIControlEventTouchUpInside];
AllList.tag = 0;
I would like to set the enable of this button to YES or NO in another method.
#implementation {
UIButton *myButton;
}
- (void)viewDidLoad {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.tag = 121;
myButton.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:#"allModsBtn.png"];
[myButton setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:myButton];
// add targets and actions
[myButton addTarget:self action:#selector(getButtons:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 0;
}
- (void)someOtherMethod {
myButton.enabled = YES;
OR
//In this case you dont need to define uibutton to globaly
UIButton *button = (UIButton*)[[self view] viewWithTag:121];
[button setEnabled:YES];
}
You have to make that button an ivar of your view controller.
#implementation {
UIButton *myButton;
}
- (void)viewDidLoad {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
UIImage *buttonImageFull = [UIImage imageNamed:#"allModsBtn.png"];
[myButton setBackgroundImage:buttonImageFull forState:UIControlStateNormal];
[self.view addSubview:myButton];
// add targets and actions
[myButton addTarget:self action:#selector(getButtons:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = 0;
}
- (void)someOtherMethod {
myButton.enabled = YES;
}
Something like this:
UIButton *myButton = [self.view viewWithTag:BUTTON_TAG]; // in your case is 0
[myButton setEnabled:YES];
Two ways to do it:
1) you can make it an ivar and then
AllList.enabled = YES;
or
[AllList setEnabled:YES];
2)
set a unique tag to the button
UIButton *AllList = [UIButton buttonWithType:UIButtonTypeCustom];
AllList.frame = CGRectMake(40, 80, 107.f, 53.5f); //set frame for button
AllList.tag = kUNIQUE_TAG;
in the method you want to mess with the button's enabled property
UIButton *theButton = (UIButton *)[self viewWithTag:kUNIQUE_TAG];
[theButton setEnabled:YES];

Programmatically add a UIButton to a Storyboard View

I have a Storyboard View Controller (with my own custom sub class) of which I have a initWithCoder custom method, where I want to popular the view with my own custom objects (I want to maintain the flexibility of doing this programmatically.
Code works fine until [self.view addSubview:button]; is called, then it crashes with: "Could not load NIB in bundle".
All I wanted to do was add a UIButton on my view...
- (id)initWithCoder:(NSCoder*)aDecoder
{
if(self = [super initWithCoder:aDecoder])
{
[self initLoginForm];
}
return self;
}
-(void)initLoginForm
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:#"Login..." forState:UIControlStateNormal];
[button addTarget:self action:#selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
how about putting the button in the viewDidLoad method, then add it in the initLoginForm.
Something like this:
-(void)viewDidLoad {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 170, 100, 30);
[button setTitle:#"Login..." forState:UIControlStateNormal];
[button addTarget:self action:#selector(handleLoginAttempt) forControlEvents:UIControlEventTouchUpInside];
}
-(void)initLoginForm {
[self.view addSubview:button];
}
or add it in the viewDidLoad then hide it, then in the initLoginForm show it.

custom RadioButton selected one at a time for iphone?

I have dynamically created some list of RadioButtons with some values. pro grammatically I changed the button state and changed the image for selected and unselected. but the problem is i can select the all radioButtons at same time. actually I need to select one at a time.
when I clicked the next RadioButton, previously selected button state should be changed to non-selected.
Here is my code, I tried with changing image, but ...some problem with my code.
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender ;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
where can I change the image of previously selected button.
thanks in advance
Deselect all the buttons when you select one. If you have buttons in your scroll view you can user this code:
//Your Method
-(void)RadioButtonTapped:(id)sender
{
UIButton *RadioButton1 = (UIButton*)sender;
[self deselectAll];
[self radiobuttonAction:RadioButton1];
}
- (void) deselectAll : (UIScrollView *) scrollView{
NSArray *viewArray = [scrollView subviews];
for (UIView *v in viewArray){
if([v isKindOfClass:[UIButton class]]){
[((UIButton *)v) setSelected:NO];
}
}
}
EDIT: But if you want to give it real radio button effect (in which one is always selected, and only one is selected) it will be more easy to you. Use following code:
//A globle refButton
UIButton *refButton = nil;
//Set image for both state:
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateSelected];
//make any of above default select: may be the last one and pass that to `refButton`
refButton = RadioButton;
//Your Method
-(void)RadioButtonTapped:(id)sender
{
[refButton setSelected:NO];
refButton = (UIButton*)sender;
[refButton setSelected:YES];
}
Tag the button at the time of creation.
When button tapped, get the buttons through tag. Deselect all buttons. Update current button which you have received as a n argument.
assuming you want to add 5 radio buttons.
- (void)viewDidLoad
{
for (int i = 0; i < 5; i++) {
UIButton* RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0, i * 40)];
// [RadioButton setTag:i * 10];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
}
//do not forget to declared this in your header file
-(void)RadioButtonTapped:(UIButton*)button;
{
for (UIButton *btn in self.scrollView.subviews) {
[btn setImage:[UIImage imageNamed:#"unCheck.png"] forState:UIControlStateNormal];
}
[button setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
the code above updates the UI, sets the image of all buttons to "uncheck" then changes the image of the last pressed button to "check".
create buttons like this,
for(int i=0;i<4;i++)
{
RadioButton = [UIButton buttonWithType:UIButtonTypeCustom];
RadioButton.tag = i*100;
[RadioButton setFrame:CGRectMake(0.0f, 0.0f, 20, 20)];
[RadioButton setCenter:CGPointMake(116.0,p1)];
[RadioButton setSelected:NO];
[RadioButton setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
[RadioButton addTarget:self action:#selector(RadioButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:RadioButton];
}
add action like this,
-(void)radiobuttonAction:(UIButton *)Button
{
UIButton *Button = (UIButton*)sender;
for(UIButton * btn in self.scrollview.subViews)
{
if(btn.tag == Button.tag)
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected]; //not working, button image is not changing
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio_inactive.png"] forState:UIControlStateNormal];
}
}
else
{
//do selected or de-selected code for other buttons
}
}

change image on click of button in objective c

i draged 3 button in my .xib file (btn1,btn2,btn3 respectively) and initially i given default image to them, first.png
now when user clicks on btn1, image of btn1 should change from first.png to second.png..
and when user selects on btn2, image of btn2 should change from first.png to second.png, and also change image of btn1 to default again first.png, so that user came to know he has clicked 2nd button.
how should i do this ?
Thanks In Advance !!
You have to set button action method code
-(IBAction)btnClked:(id)sender
{
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn3 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
UIButton *btn=(UIButton *)sender;
[btn setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
}
#import <Foundation/Foundation.h>
#interface CustomRadioButton : UIButton {
}
#end
#import "CustomRadioButton.h"
#implementation CustomRadioButton
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Set the refresh icon as the button's image
[self setImage:[UIImage imageNamed:#"off.png"] forState:UIControlStateNormal];
[self setImage:[UIImage imageNamed:#"on.png"] forState:UIControlStateSelected];
// When the button is pressed, draw to button with less opacity.
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
#end
inside the Implement file of the viewController
- (void)viewDidLoad {
[super viewDidLoad];
for (int i=1;i<=2;i++){
CustomRadioButton *Radiobutton = [CustomRadioButton buttonWithType:UIButtonTypeCustom];
Radiobutton = [[CustomRadioButton alloc] initWithFrame:CGRectMake(200,50*i, 30, 30)];
[Radiobutton addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
Radiobutton.tag=i;
[self.view addSubview:Radiobutton];
}
}
- (IBAction)checkboxButton:(UIButton *)button{
for (UIButton *Radiobutton in [self.view subviews]) {
if ([Radiobutton isKindOfClass:[UIButton class]] && ![Radiobutton isEqual:button]) {
[Radiobutton setSelected:NO];
}
}
if (!button.selected) {
button.selected = !button.selected;
}
}
Set the images in your button call method like:
BOOL first;
-(void)firstBtnPressed
{
if(first == YES){
[btn1 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
first = NO;
}
else
{
first = YES;
[btn1 setImage:[UIImage imageNamed:#"second.png"] forState:UIControlStateNormal];
[btn2 setImage:[UIImage imageNamed:#"first.png"] forState:UIControlStateNormal];
}
}