In some sense this question has already been answered at Limit number of characters in uitextview. But my particular case is that I have more than one textview in the same ViewController. So I am not sure how to fix that problem. Say I only have two textViews. How might I handle these cases:
they both have the same character limit?
each has different character limit? say 300 and 400 respectively.
Do I use IBAction? If yes how?
So you need IBOutlet for both textviews
#property (weak, nonatomic) IBOutlet UITextField *textfield1;
#property (weak, nonatomic) IBOutlet UITextField *textfield2;
then in your delegate method
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
You simply add a check for the right textfield
if (self.textfield1 == textfield) {
// handle first text field here
} else {
// handle second text field here
}
Related
I am using custom delegation with my UITextField in a custom UITableViewCell. Basically I have UITextFields on my UITableView, and in Edit mode, I want to be able to edit the content inside the UITextField.
My issue is that when I go into Edit mode and try to type something in the text field, I am only able to type one character and it doubles. In this example, I tried to type "p" and it only lets me type "p" and it changes to "pp".
Here's my code. As an FYI I am pretty new to iOS/programming/custom delegation/custom UITableViewCells.
UITableViewCell subclass .h:
#protocol MenuTableViewCellDelegate <NSObject>
- (void)updatedFoodItem:(NSString *)foodItem fromCell:(id)sender;
#end
#interface MenuTableViewCell : UITableViewCell <UITextFieldDelegate>
#property (nonatomic, weak) id <MenuTableViewCellDelegate> delegate;
#property (weak, nonatomic) IBOutlet UITextField *foodTextField;
#property (weak, nonatomic) IBOutlet UITextField *priceTextField;
#end
UITableViewCell .m:
- (void)awakeFromNib {
self.foodTextField.delegate = self;
}
// Delegate method for tableview cell - food item text field is within cell.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self.delegate updatedFoodItem:string fromCell:self];
return YES;
}
In my ViewController.m:
- (void)updatedFoodItem:(NSString *)foodItem fromCell:(MenuTableViewCell *)cell
{
NSIndexPath *p = [self.menuTableView indexPathForCell:cell];
id item = self.menuItemsArray[p.row];
cell.foodTextField.text = foodItem;
}
By the way I realize that the above probably isn't updating my data yet, I haven't figured it out yet so any advice would be great.
The problem is that you're calling your updatedFoodItem: method and then returning YES in your shouldChangeCharactersInRange: function. Essentially what's happening is:
1 - shouldChangeCharactersInRange: is called
2 - This function calls updatedFoodItem:
3 - The updatedFoodItem: function sets the cell's text field to the original text passed in
4 - The shouldChangeCharactersInRange: method returns YES, appending the same text that was just set in the updatedFoodItem: function to the cell's label
I'm not sure exactly what you're trying to do (maybe you're trying to save the food item into your self.menuItemsArray?), but you should either not set the cell's text in the updatedFoodItem: function, or you should return NO in your shouldChangeCharactersInRange: function.
I am stuck in a very strange problem. A default text field in iOS 8 & 7 behave very strange.
If the text box got focused the alignment of text get changed check the screen shots.
even with very small text size
Edit:
#import <UIKit/UIKit.h>
#interface LoginForm : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *txt_username;
#property (weak, nonatomic) IBOutlet UITextField *txt_password;
- (IBAction)loginButtonPressed:(id)sender;
#end
Try this. Its more of a work around, not exactly getting to the root of the problem but make your VC a UITextFieldDelegate, then make it the textFields delegate and use the following code. -
(void)textFieldDidBeginEditing:(UITextField *)textField
{
// Change the alignment if you need to.
textField.textAlignment = NSTextAlignmentCenter;
}
It should be a alignment issue. In your storyboard check the UITextField Alignment matches with the below image.
- (IBAction)SignUp:(id)sender; { Expected identifier or '('
IBOutlet UITextField *Firstnamefield;
IBOutlet UITextField *Lastnamefield;
IBOutlet UITextField *emailfield;
IBOutlet UITextField *agefield;
IBOutlet UITextField *passwordfield:
IBOutlet UITextField *reenterpasswordfield;
}
- (IBAction)SignUp:(id)sender {
UITextField *Firstnamefield;
UITextField *Lastnamefield;
UITextField *emailfield;
UITextField *agefield;
UITextField *passwordfield;
UITextField *reenterpasswordfield;
}
There a couple of problems. For one, you can not use the IBOutlet qualifier outside of an interface declaration. And passwordfield has a colon after it that should be a semicolon.
In case this is the point of confusion, if you were creating the interface declaration for this IBAction, it would look like this:
- (IBAction)SignUp:(id)sender;
Other than that, the only other thing that could be causing this is if you are trying to place the entire IBAction inside the header file.
It seems to me that you put an IBAction at the wrong place in the .h file.
It should be after the definition of the instance variables, for example:
#interface ViewController : UIViewController
{
IBOutlet UITextField *Firstnamefield;
IBOutlet UITextField *Lastnamefield;
// ...
}
- (IBAction)SignUp:(id)sender;
#end
Hopefully somebody can help me out here. I have a UIView with 3 text fields. I want the "return" key from the keyboard to resign the first responder, regardless of which text field the users curser is in. My code works for 2 of the 3 text fields, but consistently (as in always) it refuses to resign first responder from the 3rd text field, and I don't know why (the text fields are identical. The field that does not work is the 3rd field...
Here is the code if it helps
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[firstTextField resignFirstResponder];
[secondTextField resignFirstResponder];
[thirdTextField resignFirstResponder];
return YES;
}
Here is the property declaration in the H
#property (weak, nonatomic) IBOutlet UITextField *firstTimeTextField;
#property (weak, nonatomic) IBOutlet UITextField *secondTextField;
#property (weak, nonatomic) IBOutlet UITextField *thirdTextField;
It seems pretty straight forward, so I'm wondering if there is some kind of error checking that I should be doing to determine whats happening here.
The file is declared as a UITextFieldDelegate as well...
Thanks for the time and the assistance!
Have you set the delegate to ALL UITextFields?
Set the delegate to all UITextFields.
I have a popover that accepts input into a UITextField. Depending on the input, I set the properties: text, keyboardType, returnKeyType, and keyboardAppearance. The text and returnKeyType properties are reflected in the popover. But I get a standard keyboard, with numbers (UIKeyboardTypeNumberPad, UIKeyboardTypeDecimalPad) or letters (UIKeyboardTypeDefault) showing. I expected to see the "enter pin" keyboard for UIKeyboardTypeNumberPad and numbers with a period for the UIKeyboardTypeDecimalPad keyboardType.
My code...
.h:
#interface IFDTextPopoverContentViewController : UIViewController {
UILabel *notes;
UITextField *input;
}
#property (nonatomic, retain) IBOutlet UILabel *notes;
#property (nonatomic, retain) IBOutlet UITextField *input;
.m:
IFDTextPopoverContentViewController *textPopover = (IFDTextPopoverContentViewController *)textPopoverController.contentViewController;
textPopover.input.text = [xmlResults valueForKey:question.XmlAttrib];
textPopover.input.keyboardType = UIKeyboardTypeNumberPad;
textPopover.input.keyboardAppearance = UIKeyboardAppearanceDefault;
textPopover.input.returnKeyType = UIReturnKeyDone;
textPopover.notes.text = question.Notes;
Am I missing something to change the keyboardType? I am currently running this on an iPad. I have not tested on an iPhone.
Another option might be to add a numeric or numeric+decimal keypad to the popover, but I do not know where to start on this option.
The iPad has no keypad keyboard. So it shows you the normal number keyboard instead.