Custom Button Action - ios

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

Related

Select/Deselect for dynamically created UIButtons

I will explain the Scenario:
I am dynamically creating three UIButtons inside a for loop.The condition is by default the first button should be bordered.Now if we select other buttons the previous button's border should go and now the border should be present for selected button.
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
self.graphSubDataButton = [[UIButton alloc] init];
self.graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[self.graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[self.graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
self.graphSubDataButton.tag = btnsCount+1;
[self.graphSubDataButton addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
if (self.graphSubDataButton.tag==1)
{
[self.graphSubDataButton.layer setBorderWidth:1.2f];
[self.graphSubDataButton.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[self.graphSubDataButton.layer setCornerRadius:8.0f];
}
}
-(void) enableRespectiveButton2:(UIButton *) sender
{
}
I have changed your code structure little to make it work properly, you can comment if you don't understand something.
NSInteger defaultSelectedTag = 1;
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
UIButton *graphSubDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
graphSubDataButton.tag = btnsCount+1;
[graphSubDataButton addTarget:self action:#selector(enableRespectiveButton:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
//add your button somewhere
//e.g.
//[self.view addSubview:graphSubDataButton];
if (graphSubDataButton.tag == defaultSelectedTag) {
[self setBorderForButton:graphSubDataButton];
}
}
- (void) setBorderForButton:(UIButton *)sender {
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[sender.layer setCornerRadius:8.0f];
}
- (void) enableRespectiveButton2:(UIButton *) sender {
for(UIButton *buttons in [sender.superview subviews]) {
if([buttons isKindOfClass:[UIButton class]]) {
if(![buttons isEqual:sender]) {
//add logic to, remove borders
//buttons.layer.borderWidth = 0.0f;
}
}
}
[self setBorderForButton:sender];
}
Use tag to get current selected button or much simple: iterate through all subviews and set border to 0 for UIButtons
for (UIView *subview in [uiv_ButtonsView subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
subview.layer setBorderWidth:0.0f;
}
}
then use:
UIButton *button=(UIButton *)sender;
[button.layer setBorderWidth:1.2f];
[button.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[button.layer setCornerRadius:8.0f];
Create dynamic buttons like :
int buttonsOffset = 50;
for (int btnsCount = 0 ; btnsCount <3; btnsCount++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor blueColor];
btn.frame = CGRectMake(buttonsOffset, 100, 120, 40);
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
btn.tag = btnsCount+1;
[btn addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + btn.frame.size.width;
if (btn.tag==1)
{
[btn.layer setBorderWidth:1.2f];
[btn.layer setBorderColor:[UIColor greenColor].CGColor];
[btn.layer setCornerRadius:8.0f];
self.graphSubDataButton = btn;
}
[self.view addSubview:btn];
}
And click event of button is as following
- (void)enableRespectiveButton2:(UIButton *)sender
{
sender.selected = TRUE;
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColor greenColor].CGColor];
[sender.layer setCornerRadius:8.0f];
self.graphSubDataButton.selected = FALSE;
[self.graphSubDataButton.layer setBorderWidth:0.0f];
[self.graphSubDataButton.layer setBorderColor:[UIColor clearColor].CGColor];
[self.graphSubDataButton.layer setCornerRadius:0.0f];
self.graphSubDataButton = sender;
}
Create dynamic buttons with for loop as shown below.
for(int i = 0 ;i <3; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString * title = [NSString stringWithFormat:#"Button %d",i];
[btn setTitle:title forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"BorderImg"] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:#"UnBordered"] forState:UIControlStateNormal];
if(i == 0) [btn setSelected:YES];
else [btn setSelected:NO];
[btn setTag:i];
[btn addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}
Have 2 images
Highlighted border (BorderImg)
Normal border (UnBordered)
Set those images to button while creating in for loop
- (void)btnTapped:(UIButton *) btn {
if(! btn.isSelected) {
[btn setSelected:YES];
}
Inside button selector method, based on the sender selection state , update the button selected state. (In side selector method i.e btnTapped set other buttons selected state to false)

IOS UIButton with IBAction

I have create some buttons on for loop on below code.
UIView *buttonsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _viewEmoji.frame.size.width, 35)];
[buttonsView setBackgroundColor:[UIColor greenColor]];
for (int i = 0; i < myObject.count; i ++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(35*i + 5, 0, 35, 35)];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [myObject objectAtIndex:i]]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[buttonsView addSubview:button];
}
And now, How to I can click button to handle event.
Every button was clicked, It will handle 1 event.
Ex: if I have 2 buttons was created by for loop. When I click button 1, it's will log 1, When I click button 2, it's will log 2.
do like
UIView *buttonsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _viewEmoji.frame.size.width, 35)];
[buttonsView setBackgroundColor:[UIColor greenColor]];
for (int i = 0; i < myObject.count; i ++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(35*i + 5, 0, 35, 35)];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#", [myObject objectAtIndex:i]]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
// set the tag for identify which button you pressed
button.tag = i; // else use --> [myObject objectAtIndex:i]
[buttonsView addSubview:button];
}
//here add your buttonsView to mainview
self.view addsubview(buttonsView)
// for button action
-(void)clickButton:(UIButton*)sender
{
NSLog(#" Index: %d ", sender.tag);
[sender setBackgroundImage:[UIImage imageNamed:#"XXX.png"] forState:UIControlStateNormal];
}

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

programmatically added button should move to another ViewController

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

I have to perform togling for single UIButton which I have generated from loop for different frame

In my iOS application, I have to change image as per button click.
I have to perform togling for single UIButton which I have generated from loop for different frame
It is working fine if I perform action on single button, if I have performing action for multiple button the image is not properly changing as per button action
Here is my button
btnFullScreen = [[UIButton alloc] init];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[btnFullScreen setFrame:CGRectMake(self.view.frame.size.width-30,8,16,14)];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[btnFullScreen addTarget:self action:#selector(showPopUp:) forControlEvents:UIControlEventTouchUpInside];
btnFullScreen.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:btnFullScreen];
Here is my button action code
-(void)showPopUp:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
[self.parent.parent addPopOversExcept:self.parent];
}
Thanks
I had done this before in one of my app.
I was creating button in each tableview cell.
So,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";
}

Resources