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;
}
Related
Actually I just want to make it like:
When user taps button 1 , it stores its value in a string and when user selects another button; it also store its value also and when he de-select the button again it must be removed from string and also from uiLabel
How it will be possible ?
I have to know all possible ways
1) Declare in your ViewController.m
#interface ViewController ()
{
UIButton *button1;
UIButton *button2;
NSString *btn1String;
NSString *btn2String;
BOOL btn1isClicked;
BOOL btn2isClicked;
UILabel *label;
}
2) Buttons and label (can be in your ViewDidLoad)
btn1isClicked = NO;
btn2isClicked = NO;
//initialy set to nothing
btn1String = #"";
btn2String = #"";
//create first button
button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 50, 200, 50)];
//button 1 clicked
[button1 addTarget:self action:#selector(button1Clicked) forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Button One" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
[self.view addSubview:button1];
//create second button
button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 150, 200, 50)];
//button 2 clicked
[button2 addTarget:self action:#selector(button2Clicked) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Button Two" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.view addSubview:button2];
//create label
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 320, 50)];
[label setText:#""];
[self.view addSubview:label];
3) Button Clicked methods
- (void) button1Clicked
{
if (!btn1isClicked)
{
//set bool to yes
btn1isClicked = YES;
[button1 setTitle:#"Button One Clicked" forState:UIControlStateNormal];
//set button 1 value
btn1String = #"btn1value";
}
else
{
//set bool to no
btn1isClicked = NO;
[button1 setTitle:#"Button One" forState:UIControlStateNormal];
btn1String = #"";
}
//update label
[label setText:[NSString stringWithFormat:#"%# %#", btn1String, btn2String]];
}
- (void) button2Clicked
{
if (!btn2isClicked)
{
btn2isClicked = YES;
[button2 setTitle:#"Button Two Clicked" forState:UIControlStateNormal];
//set button 2 value
btn2String = #"btn2value";
}
else
{
btn2isClicked = NO;
[button2 setTitle:#"Button Two" forState:UIControlStateNormal];
btn2String = #"";
}
//update label
[label setText:[NSString stringWithFormat:#"%# %#", btn1String, btn2String]];
}
Hope this helps
-(IBActions)buttonDidTap:(UIButton*)btn { //the target selector the button is attached to
if ([btn isSelected]) { //check if the button is selected
NSString *buttonTitleValue = btn.currentTitle; //Get the button title value
myLabel.text = buttonTitleValue; //set the UIlabel text to the title of the button
}
}
See: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instp/UIButton/currentTitle
I have programmatically generated buttons like this:
- (void)viewDidLoad
{
[super viewDidLoad];
int y = 127;
for (int i=0; i<=6; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
//[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(26, y, 268, 41);
[button setTitle:[NSString stringWithFormat:#"Button-%i", i] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"cys_button.png"] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeueLTPro-Cn" size:21]];
[button setTitleColor:[UIColor colorWithRed:60/255.0 green:60/255.0 blue:60/255.0 alpha:1] forState:UIControlStateNormal];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.contentEdgeInsets = UIEdgeInsetsMake(8, 15, 0, 0);
[scrollViewContent addSubview:button];
y=y+43;
}
}
and method which shows text after click:
-(void)aMethod:(id)sender{
UIButton *button = sender;
int y = 127;
int i = 6;
for (int x=0; x<=i; x++) {
NSString *frameString = [NSString stringWithFormat:#"{{26, %i}, {268, 41}}", y];
if ([NSStringFromCGRect(button.frame) isEqualToString:frameString]) {
NSLog(#"button %i clicked..", x);
}
y= y+43;
}
}
How to do that after click the button transfer occurred to the next view?
use:
button.tag=i;
set tag to the button while creating button in viewDidLoad so that you can identify your button by using the tag above.
UIButton *button=(UIButton *)[self.view viewWithTag:i];
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.
I've added a button to section header which shows a view on clicking it. My need is that I've to show an "Up-arrow" image while view is shown and "Down-arrow" image when view is hidden.
How can I achieve this? Please help me ...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 316, 60)];
[btn setTag:section];
[aView addSubview:btn];
[btn addTarget:self action:#selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
btn.backgroundColor=[UIColor clearColor];
}
I will suggest that you can have 2 images - 1 for 'Up' and 1 for 'Down' arrow.
Set default image to the btn for state UIControlStateNormal and set the other image for state UIControlStateSelected.
Now in your sectionTapped: method just change the state of the button.
So when you show or hide your view you need to set the button selected YES/NO.
Hope this helps.
I had done this before in one of my app.
Take this code as Reference
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}
You can trying using BOOL to check if the button has been pressed or not and animate to rotate the image in your button to point up or down.
-(IBAction)dropdownBtnClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (!isDropdownEnabled)
{
//BOOL - checking if the button has been pressed or not
isDropdownEnabled=TRUE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(270/180*M_PI);
}
else
{
isDropdownEnabled=FALSE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(0);
}
[UIView commitAnimations];
}
I have 42 custom button on one View. How can I by pressing any of them edit of the created buttons that i want.
int a=0; int b=1;
int otstup=10;
for (int i=1; i<=42; i++) {
CGRect frameBtn = CGRectMake(a+60+otstup, b+otstup, 45, 45);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:frameBtn];
[button setBackgroundImage:[UIImage imageNamed:#"EmptyCoin.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:i];
[self.view addSubview:button];
a=a+50;
if (i%7 == 0)
{
a=0;
b=b+45;
}
}
-(void)pressBtn:(id)sender{
UIButton *btn = (UIButton*)sender;
if (btn.tag == 1){
1st button tapped
}
else if(btn.tag == 2)
{
2nd button tapped
}
}
By using above code you can differentiate different buttons
Update
You have to create one mutable array store all the buttons in that array. you can access that array in pressBtn method
int a=0; int b=1;
int otstup=10;
for (int i=1; i<=42; i++) {
CGRect frameBtn = CGRectMake(a+60+otstup, b+otstup, 45, 45);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:frameBtn];
[button setBackgroundImage:[UIImage imageNamed:#"EmptyCoin.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:i];
[buttonAry addObject:button];
[self.view addSubview:button];
a=a+50;
if (i%7 == 0)
{
a=0;
b=b+45;
}
}
Button action method
-(void)pressBtn:(id)sender{
UIButton *btn = (UIButton*)sender;
if (btn.tag == 7){
UIButton *editButton = [buttonAry objectAtIndex:btn.tag+1];
[editButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
}
}