UIButton on UIScrollView can't work - iphone-sdk-3.0

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
myLabel.backgroundColor = [UIColor greenColor];
[self.view addSubview:myLabel];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self addTarget:self action:#selector(show) forControlEvents:UIControlEventTouchDown];
myButton.frame = CGRectMake(80.0, 120.0, 30, 30);
myButton.showsTouchWhenHighlighted = YES;
[self addSubview:myButton];
in this way the button works(call show:), but use [myLabel addSubview:myButton]; the button doesn't work. not sure why?
----------------EDIT & SOLUTION------------
Thanks #KennyTM
UILabel by default doesn't handle any events. You need to set the userInteractionEnabled property of the label to YES.
Also you'd better not add a UIButton on the top of a UILabel.

UILabel by default doesn't handle any events. You need to set the userInteractionEnabled property of the label to YES.
A button shouldn't be a subview of a label anyway, it's illogical. Make both of them subviews of a UIView.

Related

ChildView didn't maintain its ParentView Frame

Hope all of you will be fine. Guys i have a situation, I have a uiview and uibutton as under:
UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 70)];
containerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:containerView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:#selector(SettingPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:#"settings_button.png"];
[button setImage:image forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
button.frame = CGRectMake(0,0, 40, 40);
button.center = containerView.center;
[containerView addSubview:button];
The problem is uibutton appear far from uiview instead of in the middle of uiview. What i am mistaking, please guide me.
Thanks.
First, when you change a view's center property the frame property will be changed automatically so you only should set the button center:
button.center = CGPointMake(containerView.frame.size.width / 2,
containerView.frame.size.height / 2);
Now, whenever you change the center property you need to manually tell the view to redraw itself using setNeedsDisplay :
[button setNeedsDisplay]
and lastly, the difference between frame and bounds that frame defines the position and size relative to the parent view while bounds describes the drawing area inside (or outside) the frame.

Program a UIButton to change UILabel

I am trying to use Objective-C in iOS Single View app to program a UIButton to change the text in a UILabel. Is there a way to run a function for this to be implemented in my "ViewController.m" file?
Here is my Source Code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)loadView {
//UIBUTTON
//allocate the view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//set the view's background color
self.view.backgroundColor = [UIColor whiteColor];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(50, 280, 280, 50);
//set the font size of the button
button.titleLabel.font = [UIFont systemFontOfSize:40];
//set the text color of the button, before it is pressed
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//set the text color of the button, while it is pressed
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
//set the button's title
[button setTitle:#"3" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[self.view addSubview:button];
//UILABEL
UILabel *Question=[ [UILabel alloc] initWithFrame:CGRectMake(10,200,350,60)];
Question.text=#"How many horns does a Triceratops have?";
Question.backgroundColor = [UIColor grayColor];
Question.textColor = [UIColor whiteColor];
Question.font=[UIFont fontWithName:#"Helvetica" size:18 ];
[self.view addSubview:Question];
}
-(void)buttonPressed { }
#end
Is it possible to trigger the change in text in my buttonPressed function? Thanks so much for anybody's help!
The problem is that when you create your label and add it to the interface...
UILabel *Question=[ [UILabel alloc] initWithFrame:CGRectMake(10,200,350,60)];
// ...
[self.view addSubview:Question];
...you didn't keep a reference to it. So now, in buttonPressed, you have no way to talk about the label.
Make a property:
#interface ViewController ()
#property (nonatomic, weak) UILabel* questionLabel;
#end
... and when you create the label, remember to set that property to that label. Now you will be able to access the text of that label later on, in buttonPressed (or anywhere else), through the property.

textfield for ios

i am very new to iOS and xcode. i want to create a textfield without using nib files or storyboard. i searched for the answers but everywhere its kind of confusing or they have used nib files. i have created buttons and its working fine but for textfields m facing problems. here is my viewcontroller.m please help.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor purpleColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextField *textfield;
button.frame = CGRectMake(10 ,350 , 300, 40);
button1.frame = CGRectMake(10 ,50 , 300, 40);
textfield.frame = CGRectMake(10, 100, 200, 30);
[button setTitle:#"Change screen to green!" forState:UIControlStateNormal];
[button1 setTitle:#"click screen to red!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self action:#selector(buttonPressed1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[self.view addSubview:button1];
[self.view addSubview:textfield];
}
-(void)buttonPressed {
self.view.backgroundColor = [UIColor greenColor];
}
-(void)buttonPressed1 {
self.view.backgroundColor = [UIColor redColor];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#end
thanks & regards
You just declared the UITextField instead of allocating. You must allocate it first then set frame & add it on the view like -
UITextField *txt = [[UITextField alloc] init];
txt.frame = CGRectMake(10, 100, 200, 30);
txt.delegate = self;
[self.view adsubview:txt];
It solves your problem.
First use
UITextField *textfield = [[UITextField alloc] init];
instead of
UITextField *textfield;
Then set backgroud color of textfield since the backgroud color will be clearColor by default.
textfield.backgroundColor = [UIColor whiteColor];
or use
textfield.borderStyle = UITextBorderStyleRoundedRect;
to get white colored bordered text field.
If you've added the alloc init like the answers have suggested, your text field is in fact on the screen, but since it has no border or no text, you can't see it. Try adding this line, and then you will see it:
textfield.borderStyle = UITextBorderStyleRoundedRect;
Use this:
UITextField *textfield = [[UITextField alloc] init];
Insetd of
UITextField *textfield;
UITextField *textfield; Just crete object reference Not object.
UITextField *textfield =[[UITextField alloc] init];
textfield.frame = CGRectMake(10, 100, 200, 30);
textfield.delegate = self; [self.view addSubview:textfield];
[self.view bringSubViewToFront:textfield];
may solve the problem

Is it possible to add a UILabel to UITableView Cell which has already UIButton as Subview?

Hii is this possible to add UILabel to UITableView Cell which has UIButton as Subview. I meant this UILabel must be added to the UIButton as the DetailLabel i.e below the title of the Button?
E.x My coding is that
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[button setImage:[UIImage imageNamed:#"launchpad.png"]forState:UIControlStateNormal];
[button setTag:001];
[button addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
Below the Title text i want the label as the subview to load the dynamic text at runtime.
Hope i explained clearly. Please help me with the coding and the way to add UILabel.
You can add this label to UIButton as subview:
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[button setImage:[UIImage imageNamed:#"launchpad.png"]forState:UIControlStateNormal];
[button setTag:001];
[button addTarget:self action:#selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,24, 320, 20)];
label.text = #"Some text"
[button addSubview:label];
[cell addSubview:button];

UIButton IB vs programmatically

When I make a custom UIButton with a background in interface builder, it gives me this default functionality of when i touch up inside, it sort of blacks out the image.
If I create the UIButton programmatically, like this:
buttonPic = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 63)];
[[buttonPic imageView] setContentMode:UIViewContentModeScaleToFill];
[buttonPic setImage:[video pic] forState:UIControlStateNormal];
it doesn't behave like the one I created in IB at all. Nothing changes when I touch up inside.
Is there a settings in UIButton that I am missing?
Thanks
This is how to create a UIButton programatically.
//Specify the type of the button first. No need to use alloc-init for UIButton like other view objects.
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
// Give UIButtonTypeRoundedRect to get default Interface builder style button.
myBtn.frame = CGRectMake(0, 0, 100, 40);
[self.view addSubview:myBtn];
// Since the button type is custom, give an image to make it visible.
[myBtn setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:normal];
[myBtn addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
// These are some optional methods you may want to use.
myBtn.titleLabel.font = [UIFont fontWithName:#"Arial" size:15];
[myBtn setTitle:#"my button" forState:UIControlStateNormal];
[myBtn setTitleColor:[UIColor blackColor] forState:normal];
[myBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
I think you must enable one of this two options:
buttonPic.showsTouchWhenHighlighted = YES;
buttonPic.reversesTitleShadowWhenHighlighted = YES;