Add UIButton programmatically from a IBAction - iphone

I am trying to make an action where every time you press a button, it adds a button to the view. Nothing I have tried works.
It would be much appreciated if someone could clear up what to do.
This is the most recent attempt at my button-adding button:
-(IBAction) addButton{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self
action:#selector(buttonTouched:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10,10,50,50);
button.tag = 1;
[button addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Try the above codes.

you need to add this
#import <QuartzCore/CoreAnimation.h>
then Do the follow this
-(IBAction)buttonTouched:(id)sender{
NSLog(#"New Button Clicked");
}
-(IBAction)addButton:(id)sender
{
UIButton *btnallstates;
UIImage *statesbtnimg=[UIImage imageNamed:#"1.png"];
btnallstates=[[UIButton buttonWithType:UIButtonTypeCustom]retain];
btnallstates.tag=1;
btnallstates.frame=CGRectMake(103, 127, 193, 31);
[btnallstates.layer setBorderWidth:0];
[btnallstates addTarget:self action:#selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[btnallstates setBackgroundImage:statesbtnimg forState:UIControlStateNormal];
[self.view addSubview:btnallstates];
}

Your code is correct. Only change this line
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
to
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
to show the button on the view.
UIButtonTypeCustom does not show up although it is added to the view.
Your can understand this by changing the color of the button.
e.g change the code to this.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor=[UIColor redColor];
Hope it works. Enjoy.

Try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];

Related

How do I get a UIButton to change its color by clicking it?

I want to make an app for iphone. It is a Nonogram something similar to this: http://www.puzzle-nonograms.com/ How can I make the UIButtons changes their color from white to black and reverse?
.h file
UIButton *button;
BOOL clicked;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
clicked=YES;
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(changeBtnColor)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#" 1 " forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
-(void)changeBtnColor{
if (clicked) {
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
clicked=NO;
}
else{
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
clicked=YES;
}
}
I hope it will help. Best of luck..
No need to ivar a boolean, buttons have selected, highlighted and normal states built into them.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0, 0, 320, 50)];
[button addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Normal State" forState:UIControlStateNormal];
[button setTitle:#"Selected State" forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"normal_image"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"selected_image"] forState:UIControlStateSelected];
[self.view addSubview:button];
}
-(void)buttonPress:(UIButton *)button{
button.selected = !button.selected;
//do your other code here for button selection
}
Otherwise set up the button states inside interface builder and just have the buttonPress selector handle the state change.

I am using same action for 5 buttons and want to know which button is called?

I am using same action for 5 buttons and want to know which button is called
In such a case please try assigning unique tags to the buttons.
In the target method regain the button tag as follows
Eg
-(void)targetMethod:(id)sender{
UIButton *button = (UIButton *)sender;
int clickedButtonTag = button.tag ;
}
Give tag value to your button in nib then add this in your button action:
allbtn = sender;
btntag = allbtn.tag;
NSLog(#"btntag:%d",btntag);
if(btntag==1)
{
}
Simple now you easily find what button you tap.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
button.tag=1;
[view addSubview:button];
-(void)aMethod:(id)sender{
UIButton *button = (UIButton *)sender;
int clickedBtnTag = button.tag ;
Nslog("clicked button tag is %d",clickedBtnTag);
}
Try this and then please revert me..
use:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
button.tag=1;
[view addSubview:button];
then use:
-(void)aMethod:(id)sender{
UIButton *button = (UIButton *)sender;
int clickedBtnTag = button.tag ;
Nslog("clicked button tag is %d",clickedBtnTag);
}
Try this and then please revert me..

UIButton - Programmatic creation issue

I am creating a UIButton programmatically as below.
- (void)viewDidLoad
{
[paymentsHomeView setFrame:CGRectMake(0, 0, 320, 520)]; // paymentsHomeView is added and referenced in IB
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
[super viewDidLoad];
}
-(void) buttonClicked {
NSLog(#"buttonClicked");
}
There is no output for the above code. The button is not created.
Whereas the same works when I create UITextField.
Help me out. Thanks in advance..!
you need to set frame for button after init statement and you need to set title for button in different way
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Button" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
Try this one my friend..
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[paymentButton setTitle:#"Show View" forState:UIControlStateNormal];
paymentButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.paymentsHomeView addSubview:paymentButton];
let me know is it working or not...!!!
Happy Coding!!!!
You are creating button here:
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
and again on this line you create another button so this overwrite the previous one, so you need to set the frame again:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
What I suggest do this:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[paymentButton setFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
I have notated some problems here:
[paymentsHomeView setFrame:CGRectMake(0, 0, 320, 520)]; // paymentsHomeView is added and referenced in IB
UIButton *paymentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 45, 300, 41)];
paymentButton.titleLabel.text = #"Button";
// You just allocated a paymentButton above. Without ARC it is leaked here:
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Now you have assigned an auto released button to paymentButton. It does not have a frame or text.
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
Try this:
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
paymentButton.frame = CGRectMake(10, 45, 300, 41);
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
For Custom Button use bellow code...
Remove this line
paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and add this line which shows button with black background
paymentButton.titleLabel.textColor = [UIColor whiteColor];// set text color which you want
[paymentButton setBackgroundColor:[UIColor blackColor]];// set back color which you want
And for RoundRect Button use bellow code
UIButton *paymentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
paymentButton.frame = CGRectMake(10, 45, 300, 41);
paymentButton.titleLabel.text = #"Button";
[paymentButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.paymentsHomeView addSubview:paymentButton];
[self.paymentsHomeView bringSubviewToFront:paymentButton];

iphone customizing UIButtons

how to customize the UIButton so that it will display other than the default button style.
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10.0, 20.0, 50.0, 50.0); // You can change the x, y, width, height.
[btn setImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[self.view addSubView:btn];
You can do something like this:
UIImage *btnImage = [UIImage imageNamed:#"images-01.png"];
UIButton *btnGo = [UIButton buttonWithType:UIButtonTypeCustom];
btnGo.frame = CGRectMake(85,123, 100, 26);
btnGo.backgroundcolor = [UIColor clearColor];
btnGo. setImage:btnImage forState: UIcontrolStateNormal];
[btnGo.titleLabel setFont: [UIFont systemFontOfSize:14]];
btnGo addTarget: self action:#selector(btnGoClicked) forControlEvents: UIControlEventTouchUpInside];
You can use this
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(899, 5, 82, 55);
customButton.tag = 123;
[customButton addTarget:self action:#selector(buttonToggleForHide) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:customButton];
and make event for that using
-(void)buttonToggleForHide{...}
In interface build simply change the type from rounded rect to custom and then specify a new background image.
In code:
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
Set the background image on it, set Type=Custom in interface builder.
Do you mean display button with custom image? You can try this:
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = urRequiredFrame;
[button setImage:yourCustomImage forState:UIControlStateNormal];
button addTarget:self action:#selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubView:button];
#shabbir 1st select the button then press cmd+1 change the button type to custom
if it right select my answer
other way is that
UIButton* b = [UIButton buttonWithType:UIButtonTypeCustom];
And if you want to call image on button then use this
UIButton *button = [[UIButton alloc]initWithImage:[UIImage imageNamed:#"button.png"]];
[button release];

Problem in UIButton in iPhone programming

Can you give me some links or example programs to create UIButtons on a view and on action on it, we need to create one more button on other view..
plz help
-(void)action:(id)sender
{
}
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
button.titleLabel.font = [UIFont systemFontOfSize: 30];
[button setTitle:#"TITLE"
forState:UIControlStateNormal];
[button addTarget:self
action:#selector(action:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];