how to remove duplicate button - ios

This is my button creation function.
-(void)buttonCreate {
//oneBtn5 Button
oneBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn5.frame = CGRectMake(316, 389, 51, 21);
oneBtn5.tag = 5;
[oneBtn5 setTitle:#"" forState:UIControlStateNormal];
[oneBtn5 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchDown];
[oneBtn5 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn5];
}
I will show button using viewonload(),
- (void)viewDidLoad {
[super viewDidLoad];
[self buttonCreate];
}
This is my restart function, if i clicked the restart, new button will create.
- (void)RestartAction {
[self buttonCreate];
}
My problem is how to remove duplicate button when i click the restart. It's there any way to solve this problem.

-(void)buttonCreate {
//oneBtn5 Button
if (oneBtn5 != null)
[oneBtn5 removeFromSuperview];
oneBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn5.frame = CGRectMake(316, 389, 51, 21);
oneBtn5.tag = 5;
[oneBtn5 setTitle:#"" forState:UIControlStateNormal];
[oneBtn5 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchDown];
[oneBtn5 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn5];
}

Just remove the button from it's superview:
-(void)buttonCreate {
if (oneBtn5.superView) {
[oneBtn5 removeFromSuperview];
}
....
}

you could do this in buttonCreate
if(oneBtn5==nil)
{
oneBtn5 = [UIButton buttonWith....];
[self.view addsubview: oneBtn5;
}
button only inits and adds once

Use this as :
-(void)buttonCreate {
if ([self.view.subviews containsObject:oneBtn5]) {
[oneBtn5 removeFromSuperview];
}
// YOUR CODE HERE
}

If it is a property, as people tell you, check if it exists previously.
if(!self.oneBtn5){
//instance of button
self.oneBtn5 = [UIButton buttonWith....];
[self.view addsubview: self.oneBtn5];
}else{
//change stuff you need like text or frame
}
The problem is that you are creating new instances of the button instead of reusing the previous one.

-(void)buttonCreate {
//oneBtn5 Button
if (oneBtn5 ){
[oneBtn5 removeFromSuperview];
oneBtn5=nil; // release the memory
}
oneBtn5 = [UIButton buttonWithType:UIButtonTypeCustom];
oneBtn5.frame = CGRectMake(316, 389, 51, 21);
oneBtn5.tag = 5;
[oneBtn5 setTitle:#"" forState:UIControlStateNormal];
[oneBtn5 addTarget:self action:#selector(oneButton:) forControlEvents:UIControlEventTouchDown];
[oneBtn5 setImage:[UIImage imageNamed:#"1c.png"] forState:UIControlStateNormal];
[self.view addSubview:oneBtn5];
}
You must write oneBtn = nil after [oneBtn5 removeFromSuperview] to release the memory for the previously created button. As [oneBtn5 removeFromSuperview]; remove the button from its superview but does not release memory.You must release the memory by setting it to nil.

Related

Unable to remove dynamically created UIButton inside for loop

I am working on an app where the contacts phone numbers are listed depending on the number of phone numbers stored for the contact. I need a button corresponding to every phone number. I have written a for loop with an if condition
Now when user taps on a contact with 3 numbers the button is shown perfectly then he presses back button and goes to a contact with 1 number and still all the three buttons are showed.
I have tried [btn autorelease]; [btn setEnabled:NO]; [btn removeFromSuperView];
but still the problem isn't resolved. Below is the snipped of code :
for (int i=0;i<ABMultiValueGetCount(lMap);i++) {
if (i==0) {
// UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230,110,88,35); //The position and size of the button (x,y,width,height)
[btn setTitle:#"SMS" forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(showAbout)
forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn autorelease];
}
else if (i==1) {
// UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230,150,88,35); //The position and size of the button (x,y,width,height)
[btn setTitle:#"SMS" forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(showAbout)
forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn autorelease];
}
else if (i==2) {
// UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(230,190,88,35); //The position and size of the button (x,y,width,height)
[btn setTitle:#"SMS" forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(showAbout)
forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn autorelease];
}
This should work.
// Declare a constant
int buttonTagConstant = 200;
// Before creating the buttons, delete the old ones first.
for (UIView* subV in self.view.subviews)
{
if ([subV isKindOfClass:[UIButton class]] && subV.tag == buttonTagConstant)
{
[subV removeFromSuperview];
}
}
int y_of_the_button = 110;
// Also I refactored your code as below
for (int i=0;i<ABMultiValueGetCount(lMap);i++)
{
btn.frame = CGRectMake(230, y_of_the_button, 88, 35); //The position and size of the button (x,y,width,height)
[btn setTitle:#"SMS" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(showAbout) forControlEvents:(UIControlEvents)UIControlEventTouchUpInside];
btn.tag = buttonTagConstant;
[self.view addSubview:btn];
y_of_the_button += 40;
}

how to check three button conditions

I am having three button image, I want to check the conditions between them. when I click the button 1 means background image change to blue colour. at the same time I click the button 1 it will move to normal state white colour. Same for another two buttons.For that process i am using this code. [tempBtn setSelected:! tempBtn.selected];
ButtonImageSelected = [UIImage imageNamed:#"lblue.png"];
ButtonImage = [UIImage imageNamed:#"l.png"];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setBackgroundImage:ButtonImage forState:UIControlStateNormal];
button1.tag = 1;
[button1 setBackgroundImage:ButtonImageSelected
forState:UIControlStateSelected];
button1.userInteractionEnabled = YES;
[button1 addTarget:self
action:#selector(button1Selector:)
forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:button1];
Another two buttons are same as button 1.
My condition is: when button 1 is clicked means button 3 should not change. when button 3 is clicked means button 1 should not change.button 2 can select in both the condition.I am using below code to check the conditions but its not working correct.
- (void)select_id:(UIButton *)tempBtn {
if (tempBtn.tag == 1) {
button3.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
} else if (tempBtn.tag == 3) {
button1.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
}
}
help me to how to check the conditions.In the above method.
I believe what you are trying to accomplish is to have each button change states as you press them. Once you have defined your buttons, this is how you can ensure they alternate images for each state.
Alternating Images Based on Button Presses
-(IBAction)buttonPressed:(id)sender {
switch ([sender tag]) {
case 1:
if ([UIImagePNGRepresentation(button1.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default1. png"])]) {
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateNormal];
} else {
[button1 setImage:[UIImage imageNamed:#"default1.png"] forState:UIControlStateNormal];
}
break;
case 2:
if ([UIImagePNGRepresentation(button2.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default2. png"])]) {
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateNormal];
} else {
[button2 setImage:[UIImage imageNamed:#"default2.png"] forState:UIControlStateNormal];
}
break;
case 3:
if ([UIImagePNGRepresentation(button3.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default3.png"])]) {
[button3 setImage:[UIImage imageNamed:#"selected3.png"] forState:UIControlStateNormal];
} else {
[button3 setImage:[UIImage imageNamed:#"default3.png"] forState:UIControlStateNormal];
}
break;
default:
break;
}
}
Note: make sure you add the action to each button, and change the names of the images
Adding the Actions to Each Button
[button1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button3 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
Ensure you have done the following:
Added action to each button
Tagged each button accordingly
Set the initial images of each button to match the condition
Try this. Before that make sure the tags are proper for every button
- (void)select_id:(UIButton *)tempBtn {
if (tempBtn.tag == 1) {
button3.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
} else if (tempBtn.tag == 3) {
button1.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
}
else{
button1.userInteractionEnabled = YES;
button2.userInteractionEnabled = YES;
[tempBtn setSelected:!tempBtn.selected];
}
}

how to load same tableview with different NSArrays depending on uibuttons tapped?

In my app i have a scroll-view and a table-View added as a sub-view and on this scroll view i have multiple menu buttons (UI Button) added as sub-view to my scroll-view.i want to load the table-View with different arrays depending on respective menu button is tapped.Any Ideas?
- (void)viewDidLoad
{
[super viewDidLoad];
[self PutButtoninScrollView:7];
}
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
myButton1=[[UIButton alloc]init];
myButton1= [UIButton buttonWithType:UIButtonTypeCustom];
myButton1.frame=CGRectMake(5, 5, 70, 30);
[myButton1 setTitle:#"दुनिया" forState:UIControlStateNormal];
[myButton1 setTag:1];
myButton1.backgroundColor=[UIColor blackColor];
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton1];
myButton2=[[UIButton alloc]init];
myButton2= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2.frame=CGRectMake(80, 5, 70, 30);
[myButton2 setTitle:#"India" forState:UIControlStateNormal];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton2];
[self.myScrollView setContentSize:CGSizeMake(160, 35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([myButton.currentTitle isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([myButton.currentTitle isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
problem is when i execute the code
"successful" is displayed. that means ListViewAction method is getting called but my if statement is never executed.
try this
-(void)PutButtoninScrollView:(int)numberOfbuttons
{
xPosition=5;
for(int i=0;i<numberOfbuttons;i++)
{
myButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame=CGRectMake(xPosition, 5, 70, 30);
myButton.backgroundColor=[UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
[myButton setTag:i];
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myScrollView addSubview:myButton];
xPosition+=75;
}
[self.myScrollView setContentSize:CGSizeMake(xPosition,35)];
_myScrollView.backgroundColor=[UIColor redColor];
[self.view addSubview:self.myScrollView];
}
- (void) buttonClicked:(id) sender
{
NSString *strTitle = [sender currentTitle];
// arrItem have list of array items
NextView *detailViewController = [[NextView alloc] initWithNibName:#"NextView" bundle:nil];
detailViewController.arrTableView =[arrItem objectAtIndex:[strTitle integerValue]];
[self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)ListViewAction:(id)sender
{
NSLog(#"sucessful");
if ([[sender currentTitle] isEqualToString:#"दुनिया"])
{
NSLog(#"successful again 1");
}
else if ([[sender currentTitle] isEqualToString:#"भारत"])
{
NSLog(#"successful again 2");
}
}
try this
[myButton1 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];
[myButton2 addTarget:self action:#selector(ListViewAction:) forControlEvents:UIControlEventTouchUpInside];

IBAction & Buttons programmatically

I'm creating buttons programmatically and I then want to add functionality whereby when they are tapped on/pressed, they stay highlighted unless tapped on again. What I'm doing now is creating the buttons and then trying to add an IBAction. However, the issue is I have my button creation in a method and then I'm not sure how to reference the button in my IBAction. Here's my code:
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender{
//Not sure what to do here, since this method doesn't recognize testButn, How do I reference testButn
The sender is testButn. You should change stayPressed's argument type from (id) to (UIButton *)
Unless an action method is connected to several different classes of objects, it's best to replace the id with whatever object class you're using. This can be useful if you're hooking things up in IB, since it won't let you hook it to the wrong kind of object.
The fact that it's not working isn't because it doesn't recognize your button. Your approach is wrong. I think you need to have an action connected to touch down, and maybe set the selected state to YES. In your button definition, you need to set an imageForState: for the selected state. The way you're doing it now, that method isn't called until touch up.
Something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"New_PICT0019.jpg"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"New_PICT0002.jpg"] forState:UIControlStateSelected];
[testButn addTarget:self action:#selector(stayPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:testButn];
}
-(void)stayPressed:(UIButton *) sender {
if (sender.selected == YES) {
sender.selected = NO;
}else{
sender.selected = YES;
}
}
You need to cast sender to UIButton.
- (IBAction)staypressed:(id)sender
{
UIButton *theButton = (UIButton*)sender;
//do something to theButton
}
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
testButn.tag = 1;
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender
{
if ([sender tag]==1)
{
somecodes...
}
}

Checkbox control for iOS application

Is there any checkbox control in the object library for iOS applications? The nearest thing I see is the switch control, which can take a boolean value.
You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :
[button setImage:[UIImage imageNamed:#"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"unselected.png"]
forState:UIControlStateNormal];
And in the touch up inside action :
- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}
//define property of button
Declare Unchecked Image
- (void)viewDidLoad
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
Checked Image.
- (IBAction)buttonTapped:(id)sender
{
if (_checkboxButton.selected == YES)
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateSelected];
_checkboxButton.selected = NO;
}
else
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
_checkboxButton.selected = YES;
}
}
This is the code I use for two checkboxes on the screen. (I put them at the bottom of two pictures that are also on the screen. I use the UIControlEventTouchDown to call the checkBoxTapped method. This method talks to my main view controller where I decide if the answer was correct or incorrect. Then the main view controller calls back the this view and tells it to change the blank textbox to either a check:
or an x
// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:#"Checkbox"];
self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxLeft setTitle:#"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxRight setTitle:#"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
- (void)checkBoxTapped:(UIButton *)buttonPressed {
[[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}
- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {
if ( [checkboxToHighlight isEqualToString:#"left"] ){
[self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
} else {
[self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
}
}

Resources