When I click inside text box, How can I add plus button to UITextField as shown in the following screen? When this button is clicked it would launch iPhone contact application. I would very much appreciate any code example. Thanks
Following code use for adding button in UITextField.
-(void)ViewDidLoad{
UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
txt.returnKeyType = UIReturnKeyDone;
txt.placeholder = #"Enter or Mobile Number";
[txt setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"crossImg.png"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:#selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
txt.rightView = button;
txt.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txt];
}
-(IBAction)clearText:(id)sender{
}
You can use the below code for this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == yourFirstTextField)
{
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
}
}
You can use the above method if you added the field in IB.
If you are created it through code, you need to add the below code when creating:
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addImage.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside];
textField.rightViewMode = UITextFieldViewModeWhileEditing;
textField.rightView = addButton;
Use following method for add UIButton
[self.txtField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (BOOL) textFieldDidChange:(UITextField *)textField
{
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(self.txtTag.bounds.size.width - 50, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtTag addSubview:btnColor];
return YES;
}
Or Write
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
// self.scrollView.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 250);
[UIView commitAnimations];
UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom];
[btnColor addTarget:self action:#selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside];
btnColor.frame = CGRectMake(150, 5, 25, 25);
[btnColor setBackgroundImage:[UIImage imageNamed:#"PaintPickerButton.png"] forState:UIControlStateNormal];
[self.txtField addSubview:btnColor];
return YES;
}
Use rightView property of UITextField.
In textFieldDidBeginEditing: delegate create button with action you want & set to rightView.
And in textFieldDidEndEditing: set nil to rightView
you can also go for custom view ant then add sub view to it. then you hide it. it will be visible only when the user taps it
self.locationName.clearButtonMode = UITextFieldViewModeWhileEditing;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 30, 30);
[button setImage:[UIImage imageNamed:#"goButtonIcon.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.locationName.rightView = button;
self.locationName.rightViewMode = UITextFieldViewModeUnlessEditing;
and then call this method:
-(IBAction)goButtonClicked:(id)sender{
}
self.locationName -------> UITextField Reference (in ViewDidload you can write this code)
Related
I had an application in which I am adding some dynamic number of buttons to a scrollview. I had set a normal background and selected background for the UIButton. For some reasons I need to call the UIButton sender method programmatically by:
[self buttontapped:nil];
as this but it is not changing the background of the button by using the code:
button.selected = YES;
I had set the background like this initially of the button:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
int j = i+1;
btn.frame = CGRectMake((j-1)*77, 0, 77, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.selected = NO;
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[tabBarS addSubview:btn];
-(void)buttonTapped:(id)sender {
if(sender==nil)
{
btn.tag=0;
}
for(int i=0;i<[sarray count];i++)
{
btn.selected=NO;
}
btn = (UIButton *)sender;
NSLog(#"Tab bar %d is clicked",btn.tag);
[self tabCall:btn.tag];
btn.selected = YES;
}
But everything except change of background only working. Where am I going wrong?
i done with bellow method call your UIButton method like:-
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*77, 0, 77, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.selected=NO;
[btn addTarget:self action:#selector(yourButtonAction:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
btn.tag = i;
[tabBarS addSubview:btn];
and it's Action Method should like with sender
-(IBAction)yourButtonAction:(id)sender
{
UIButton *btn = (UIButton*)sender;
if (![btn isSelected])
{
[btn setImage:[UIImage imageNamed:#"selected_fb_btn.png"] forState:UIControlStateNormal];
[btn setSelected:YES];
[self login];
}
else{
[btn setImage:[UIImage imageNamed:#"facebook.png"] forState:UIControlStateNormal];
[btn setSelected:NO];
}
}
EDIT
You can call button action event programeticuly using bellow trik:-
[btn sendActionsForControlEvents:UIControlEventTouchUpInside];
write this bellow line first and also change the method type from void to IBAction...
btn = (UIButton *)sender;
like bellow...
-(IBAction)buttonTapped:(id)sender {
btn = (UIButton *)sender;
if(sender==nil)
{
btn.tag=0;
}
for(int i=0;i<[sarray count];i++)
{
[btn setSelected:NO];
}
NSLog(#"Tab bar %d is clicked",btn.tag);
[self tabCall:btn.tag];
[btn setSelected:YES];
}
You need to set image property rather than backgroundImage property of UIButton.
Use default Image property and selected property.
[btn setSelected:TRUE];
is the log that you are printing, prints the button.tag correctly?
NSLog(#"Tab bar %d is clicked",btn.tag);
In your button action
btn.selected=YES;
in last line makes the button in selected state always
also get the correct instance as
btn=(UIButton *)sender;
Hi I have a UIButton that I created programmatically but unfortunately its not working. When I click on the UIButton nothing happens. I don't know what is the problem. Any help would be appreciated.
UIButton *connectedStories = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
connectedStories.frame = CGRectMake(10, 10, 1900, 30);
connectedStories.backgroundColor = [UIColor whiteColor];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
[label1 setText:storyDescription];
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
[viewContainer addSubview:contentView];
return contentView;
- (void)buttonClicked
{
NSLog(#"button clicked");
}
I think labels recives touches instead of button.
Add userInteractionEnabled = NO; to your labels.
label.userInteractionEnabled = NO;
label1.userInteractionEnabled = NO;
Try changing the top part of your code to this:
CGRect frame = CGRectMake(10, 10, 1900, 30);
UIButton *connectedStories = [[UIButton alloc] initWithFrame:frame];
//set the position of the button
[connectedStories setBackgroundColor:[UIColor whiteColor]];
[connectedStories setTitle:#"Button" forState:UIControlStateNormal];
[connectedStories setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[connectedStories addTarget:nil action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
It may very well be the following:
[contentView addSubview:backGroundImageView];
[contentView addSubview:connectedStories];
[contentView addSubview:label];
[contentView addSubview:label1];
im sure you would need to add them in the following order:
[contentView addSubview:backGroundImageView];
[contentView addSubview:label];
[contentView addSubview:label1];
[contentView addSubview:connectedStories];
This is because when you call addSubview: it adds it to end of the receivers list of subViews and the most recent subView appears on top.
Check out the UIView Documentation here
the reason that the Interaction isn't being detected is that you are not passing the touchEvents through to the other subViews as they are being captured by label1 and stopping there.
By default the last subView captures all touch events and doesn't pass them on.
Add the colon in last of your method in selector
[connectedStories addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[label setText:storyTitle];
UIButton *custombutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[custombutton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[custombutton setTitle:#"Click" forState:UIControlStateNormal];
custombutton.frame = CGRectMake(80.0, 110.0, 160.0, 40.0);
custombutton.titleLabel.textColor = [UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1];
[custombutton setImage:[UIImage imageNamed:#"hh.png"] forState:UIControlStateNormal];
[view addSubview:custombutton];
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can i add more than 10 buttons on a navigationbar in iphone application development?
I want to set an UIScrollView on a navigation bar which containing eight buttons and this navigation bar also containing leftbaritem and rightbaritem when i will press the next or previous button then scrollview have to scroll with this button because some of button will be out of the scrollview frame.
Until now i have made scroll view and buttons but problem is button are now displaying.Can anybody help me by giving an example or sending source code.
Thanks in Advance
EDIT
topToolBar = [UIToolbar new];
[topToolBar sizeToFit];
topToolBar.frame = CGRectMake(0,0, 280, 50);
topToolBar.barStyle = UIBarStyleBlackTranslucent;
segmentedControllerView = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 280, 50)];
segmentedControllerView.clipsToBounds = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0,0, 60.0, segmentedControllerView.frame.size.height -10);
button.clipsToBounds = YES;
[segmentedControllerView addSubview:button];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 addTarget:self
action:#selector(aMethod2:)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:#"Show View2" forState:UIControlStateNormal];
button2.frame = CGRectMake(60,0, 60.0, segmentedControllerView.frame.size.height - 10);
[segmentedControllerView addSubview:button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button3 addTarget:self
action:#selector(aMethod3:)
forControlEvents:UIControlEventTouchDown];
[button3 setTitle:#"Show View" forState:UIControlStateNormal];
button3.frame = CGRectMake(120,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button3];
UIButton *button4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button4 addTarget:self
action:#selector(aMethod4:)
forControlEvents:UIControlEventTouchDown];
[button4 setTitle:#"Show View" forState:UIControlStateNormal];
button4.frame = CGRectMake(180,0, 60.0, segmentedControllerView.frame.size.height -10 );
[segmentedControllerView addSubview:button4];
[topToolBar addSubview:segmentedControllerView];
self.navigationItem.titleView = topToolBar;
I am also close to the result but problem is when i am pressing the next or prev button then it moving over the next or prev button but it should be move under the view....
This is the animation code:
-(void)previousBarButtonAction{
[UIView beginAnimations: #"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x - 50,
segmentedControllerView.frame.origin.y,
segmentedControllerView.frame.size.width,
segmentedControllerView.frame.size.height);
[UIView commitAnimations];
}
-(void)nextBarButtonAction{
[UIView beginAnimations: #"moveField"context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
segmentedControllerView.frame = CGRectMake(segmentedControllerView.frame.origin.x + 50,
segmentedControllerView.frame.origin.y,
segmentedControllerView.frame.size.width,
segmentedControllerView.frame.size.height);
[UIView commitAnimations];
}
I have solve this problem slightly different way....
In viewDidLoad I have take a scrollView and called my function
menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,40)];
menuScrollView.showsHorizontalScrollIndicator = FALSE;
menuScrollView.showsVerticalScrollIndicator = FALSE;
menuScrollView.bounces = TRUE;
[self createMenuWithButtonSize:CGSizeMake(70.0, 30.0) withOffset:20.0f noOfButtons:7];
Then in my function i have created my menu buttons which will be look like buttons on a navigation bar
-(void)createMenuWithButtonSize:(CGSize)buttonSize withOffset:(CGFloat)offset noOfButtons:(int)totalNoOfButtons{
NSLog(#"inserting into the function for menu bar button creation");
for (int i = 0; i < totalNoOfButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(mybuttons:) forControlEvents:UIControlEventTouchUpInside];
if(i==0){
[button setTitle:[NSString stringWithFormat:#"Dashboard"] forState:UIControlStateNormal];//with title
}
else if(i==1){
[button setTitle:[NSString stringWithFormat:#"Order"] forState:UIControlStateNormal];//with title
}
else if(i==2){
[button setTitle:[NSString stringWithFormat:#"Product"] forState:UIControlStateNormal];//with title
}
else if(i==3){
[button setTitle:[NSString stringWithFormat:#"Customers"] forState:UIControlStateNormal];//with title
}
else if(i==4){
[button setTitle:[NSString stringWithFormat:#"Content"] forState:UIControlStateNormal];//with title
}
else if(i==5){
[button setTitle:[NSString stringWithFormat:#"Site Analysis"] forState:UIControlStateNormal];//with title
}
else if(i==6){
[button setTitle:[NSString stringWithFormat:#"Store Settings"] forState:UIControlStateNormal];//with title
}
else if(i==7){
[button setTitle:[NSString stringWithFormat:#"CMS Settings"] forState:UIControlStateNormal];//with title
}
button.frame = CGRectMake(i*(offset+buttonSize.width), 8.0, buttonSize.width, buttonSize.height);
button.clipsToBounds = YES;
button.showsTouchWhenHighlighted=YES;
button.layer.cornerRadius = 0;//half of the width
//button.layer.borderColor=[UIColor clearColor];
button.layer.backgroundColor=[UIColor blueColor].CGColor;
button.titleLabel.font = [UIFont systemFontOfSize:10];
button.layer.borderWidth=0.0f;
button.tag=i;
[menuScrollView addSubview:button];
}
menuScrollView.contentSize=CGSizeMake((buttonSize.width + offset) * totalNoOfButtons, buttonSize.height);
[self.view addSubview:menuScrollView];
}
and it solves...Happy Coding... :))
i have create a UIBUtton
editButton = [[UIButton alloc]initWithFrame:CGRectMake(width, -4, 30, 30)];
[editButton setImage:[UIImage imageNamed:#"DeleteButton.jpg"] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
editButton.hidden = YES;
[editButton setTag:tag];
[backView addSubview:editButton];
But control not jump to its definition where i check if button give its tag value on click or not.
-
(void)deleteObjectViewImage
{
NSInteger index = editButton.tag;
editButton.hidden = YES;
}
Can anyone tell me why it not work please.
Use this line instead of line in your code (colon removed after function name),
[editButton addTarget:self action:#selector(deleteObjectViewImage) forControlEvents:UIControlEventTouchUpInside];
[editButton addTarget:self action:#selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
expects the function deleteObjectViewImage: to have one argument. But the function definition doesnt has any.
UPDATE:
-(void)deleteObjectViewImage: (id) sender
{
UIButton *clickedButton = (UIButton *) sender;
NSInteger index = clickedButton.tag;
editButton.hidden = YES;
}
and use
[editButton addTarget:self action:#selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
For getting Tag Value you have to write this code in Button Click event...
-(IBAction)deleteObjectViewImage:(id)sender
{
UIButton *btn = (UIButton *)sender;
NSLog(#"%d",btn.tag);
}
Don't remove colon from
[editButton addTarget:self action:#selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
Try this
editButton = [[UIButton alloc]initWithFrame:CGRectMake(width, -4, 30, 30)];
[editButton setImage:[UIImage imageNamed:#"DeleteButton.jpg"] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(deleteObjectViewImage:) forControlEvents:UIControlEventTouchUpInside];
editButton.hidden = YES;
[editButton setTag:tag];
[backView addSubview:editButton];
- (void)deleteObjectViewImage:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"Button tag is %d",button.tag);
button.hidden = YES;
}
-(void)ViewDidLoad{
UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)];
txt.returnKeyType = UIReturnKeyDone;
txt.placeholder = #"enter name";
[txt setBorderStyle:UITextBorderStyleRoundedRect];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"icon_chapter.png"] forState:UIControlStateNormal];
button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[button addTarget:self action:#selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
txt.rightView = button;
txt.rightViewMode = UITextFieldViewModeAlways;
[self.view addSubview:txt];
}
-(IBAction)refersh:(id)sender{
}
above code shows a textfield with button at its right corner but when i clicked on it simulator crash. i can't understand whats the region behind it. any body tell where i'm wrong
Your method is spelled -refersh: while you send #selector(refresh:). Is it the typo?
Also, you seem to be leaking the UITextField, which is quite easy to fix:
[self.view addSubview:[txt autorelease]];