Problem in UIButton in iPhone programming - iphone

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

Related

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

Add UIButton programmatically from a IBAction

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

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

Anchor a function to UIButton programmatically

How can I anchor a function to my Button which I created this way
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(80.0, 160.0, 160.0, 40.0);
[button setTitle:string forState:UIControlStateNormal];
[self.view addSubview:button];
I want to do similar as it do IB when I link a function to a button.
Is it possible?
You're doing it right (assuming your aMethod gets 1 parameter - the ui control that triggers event). The only difference from action wired by default in IB - is that in IB action is set for UIControlEventTouchUpInside event.

Why can't I set my button's shadow colour?

I'm trying to set the shadow colour on a UIButton, but all I seem to be able to get is a mid grey.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(20, 20, 200, 100);
[button setTitle:#"a" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:96]];
// set up the button colours
button.titleLabel.shadowColor = [UIColor blueColor];
[button.titleLabel setShadowOffset:CGSizeMake(5.0f, 5.0f)];
[self.view addSubview:button];
alt text http://img.skitch.com/20090825-xur3112ni5q2wrwwiix4jbcwc5.png
Am I setting the wrong property, or is it the way that I'm setting shadowColor that's wrong?
Thanks.
Have you tried:
[button setTitleShadowColor:[UIColor blueColor] forState:UIControlStateNormal];