programmatically added button should move to another ViewController - ios

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

Related

Adding multiple UIButtons to an UIView with a selector action

I've added some buttons to an UIView (via addSubview) programmatically. I’ve added to this button an #selector with a function. However, they appear in the view but when I do click, the last button works only.
Into my .h:
#property (nonatomic, strong) UIButton * myButton;
Into my .m
for(int i=0;i<5;i++){
myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton addTarget:self action:#selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:myButton];
}
-(void)myaction:(UIButton *)sender{
if(sender.tag == 0){
NSLog(#“uibutton clicked %ld", (long)sender.tag);
}
}
How do I add the action to all buttons? not for the last only...
This works fine:
- (void)viewDidLoad {
[super viewDidLoad];
for(int i=0;i<5;i++){
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%ld", (long)i] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
}
-(void)myaction:(UIButton *)sender{
NSLog(#"uibutton clicked %ld", (long)sender.tag);
}
The code you posted in your question appears to be "this is what my code looks like" rather than your actual code. That can cause problems when people try to help.
In the future, post your actual code.
Let's make it more dynamic by calculating the height of the button and adding padding based on the number of buttons.
:
-(void)viewDidLoad {
[super viewDidLoad];
NSInteger height = self.frame.size.height - 5*20; // 5 is the button count
and 20 is the padding
NSInteger buttonHeight = height/5;
for(int i=0;i<5;i++){
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, buttonHeight*i+padding*(i+1), 150,
buttonHeight);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%ld", (long)i]
forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myaction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}}
-(void)myaction:(UIButton *)sender{
NSLog(#"uibutton clicked %ld", (long)sender.tag);
}
You could make it more dynamic by calculating the height of the button like this:
NSInteger height = self.frame.size.height - 5*20;
NSInteger buttonHeight = height/5;

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

How to change title color and background color of dynamic UIButton when clicked and first button should be selected as default?

I have a view and I created some 8 dynamic buttons programmatically in that. The title color of buttons is white color. I want to change color of button title to green color when it is clicked. And if i click any other button, the previously clicked button title color should become white and current button title color should become green.
I have used the following code and its working properly for changing the color of previous button and now I want the first button to be in selected mode as default and first button should be changed when another button is clicked.
- (void)viewDidLoad
{
_examsListDict1 = [[NSMutableDictionary alloc]init];
_examsListDict1[#"Exam Name"] = #"JEE Mains";
.........so on another 4 dictionaries
_examsListArray = [[NSMutableArray
alloc]initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListD
ict5, nil];
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=120;
int buttonHeight=50;
int buffer = 1;
for (i = 0; i <[_examsListArray count]; i++)
{
aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:#"Exam Name"]
forState:UIControlStateNormal];
[aButton setTag:i];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f
blue:221.0f/255.0f alpha:1]];
[aButton addTarget:self action:#selector(whatever:)
forControlEvents:UIControlEventTouchUpInside];
[_buttonsHorizontalScrollView addSubview:aButton];
xCoord += buttonWidth + buffer;
}
[_buttonsHorizontalScrollView
setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
}
- (void)whatever:(id)sender
{
UIButton *tempBtn = (UIButton *)sender;
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f
blue:221.0f/255.0f alpha:1]];
aButton = tempBtn;
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f
blue:83.0f/255.0f alpha:1]];
}
No I just want the first button to be selected as default when the view is loaded and whenever I select the other button it should change to deselected color.
if ([aButton tag] == 0)
{
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setBackgroundColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f
blue:83.0f/255.0f alpha:1]];
}
I have used the above code to select as first button as default but first button is not getting changed when other button is clicked and it is changing only when the first button is clicked again due to this aButton = tempBtn.How to do that?
I suggest you to set the selected status title color of all your buttons and then add them to an array. Then when you press one of them just change their status, no need to handle colors or keep track of the currently selected button.
#property (strong, nonatomic) NSMutableArray *yourButtons;
- (void)viewDidLoad {
[super viewDidLoad];
_examsListDict1 = [[NSMutableDictionary alloc]init];
_examsListDict1[#"Exam Name"] = #"JEE Mains";
.........so on another 4 dictionaries
_examsListArray = [[NSMutableArray alloc] initWithObjects:_examsListDict1,_examsListDict2,_examsListDict3,_examsListDict4,_examsListDict5, nil];
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=120;
int buttonHeight=50;
int buffer = 1;
// Generate background images
//
UIImage *backgroundImage = [self imageWithColor:[UIColor colorWithRed:250.0f/255.0f green:255.0f/255.0f blue:221.0f/255.0f alpha:1]];
UIImage *selectedBackgroundImage = [self imageWithColor:[UIColor colorWithRed:133.0f/255.0f green:169.0f/255.0f blue:83.0f/255.0f alpha:1]];
// Initialize your array
//
self.yourButtons = [[NSMutableArray alloc] init];
for (i = 0; i < [_examsListArray count]; i++) {
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton setTitle:[[_examsListArray objectAtIndex:i] objectForKey:#"Exam Name"] forState:UIControlStateNormal];
[aButton setTag:i];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[aButton setBackgroundImage:backgroundImage forState:UIControlStateNormal]
[aButton setBackgroundImage:selectedColorImage forState:UIControlStateSelected]
[aButton addTarget:self action:#selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[_buttonsHorizontalScrollView addSubview:aButton];
xCoord += buttonWidth + buffer;
// Select the first one
//
if (i == 0)
aButton.selected = YES;
// Add your newly created button to the array
//
[self.yourButtons addObject:aButton];
}
[_buttonsHorizontalScrollView setContentSize:CGSizeMake(aButton.frame.size.width*_examsListArray.count+203, yCoord)];
}
- (void)whatever:(id)sender {
for (UIButton *button in self.yourButtons) {
// Toggle selected flag
//
button.selected = button == sender;
}
}
+ (UIImage *)imageWithColor:(UIColor *)color {
// http://stackoverflow.com/a/24665476/1835155
//
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
TRUEThe unpressed button is UIControlStateNormal while the pressed button is UIControlStateSelected so set the colours you want based on the State. You typically do this in ViewDidload before anyone has a chance to use it - Below is a sample setup:
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setSelected:YES];
button.frame = CGRectMake(x, y, width, height);
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitle:#"Button Title" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"buttonActive.png"] forState:UIControlStateDisabled];
I believe what you should be doing when the trigger button is clicked is to just set the state to selected
- (IBAction)touchDown:(id)sender {
if (sender == trigger_button) {
[button setHighlighted:TRUE];
[button setSelected:TRUE];
}
In order to change the first button state when another button is clicked you have to "fake" a touch event. In the selector method of the other button do this:
For Example:
-(void)thirdButtonClicked:(id) sender
{
[firstButton sendActionsForControlEvents: UIControlEventTouchUpInside];
}

Delete button on UIWebView

Im getting touch location coordinates from UIWebView and displays the button with coordinates. It's working with coordinates match. So, i need to increase the button. I didn't use Array for button. When i delete particular button from UIWebView it delete all the button. Shall i use NSMutableArray?. Here x & y is the touch point coordinates in float value
if(x && y){
NSLog(#"x is %f",x);
NSLog(#"y is %f",y);
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(click1:)
forControlEvents:UIControlEventTouchDown];
[button1 setTitle:#"click" forState:UIControlStateNormal];
button1.frame = CGRectMake(x, y, 30.0, 20.0);
// [btnArray addObject:button1];
button1.tag = gTag;
gTag++;
[wbCont.scrollView addSubview:button1];
}
Delete the button:
-(void)recycle:(id)sender{
for(int i=1;i<gTag;i++){
[[wbCont.scrollView viewWithTag:i] removeFromSuperview];
}
-(void)click1:(id)sender{
UIButton *mainBtn = (UIButton *)sender;
int mainTag = mainBtn.tag;
txtview = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,568)];
txtview.font = [UIFont fontWithName:#"Helvetica" size:12];
txtview.font = [UIFont boldSystemFontOfSize:12];
txtview.backgroundColor = [UIColor whiteColor];
txtview.scrollEnabled = YES;
txtview.pagingEnabled = YES;
txtview.editable = YES;
txtview.tag = mainTag*1000;
for(int i=0; i<[textArray count];i++){
txtview.text=[textArray objectAtIndex:i];
}
[self.view addSubview:txtview];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(240.0, 20.0, 60.0, 40.0);
[txtview addSubview:button];
recyclebtn=[UIButton buttonWithType:UIButtonTypeCustom];
recyclebtn.tag = mainBtn.tag;
[recyclebtn addTarget:self action:#selector(recycle:) forControlEvents:UIControlEventTouchDown];
[recyclebtn setImage:[UIImage imageNamed:#"recycle.png"] forState:UIControlStateNormal];
recyclebtn.frame=CGRectMake(0, 10, 30, 30);
[txtview addSubview:recyclebtn];
}
-(void)recycle:(id)sender {
UIButton *btnTemp = (UIButton *)sender;
[[wbCont.scrollView viewWithTag:btnTemp.tag] removeFromSuperview];
int txtTag = btnTemp.tag*1000;
[[self.view viewWithTag: txtTag] removeFromSuperview];
}

Custom Button Action

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

Resources