Setting limits to TextBox and TextView in Xcode - iphone

I have created a Window based application with the Tab-bar as the RootViewController. On one of the tabs, I've provided a TextBox and TextView. I want to limit the number of characters that can be written inside them (i.e. TextBox and TextView).
Please help if someone knows how to do it..
Thanks..

what do you mean by "tab bar" and "tab"?
i assume you have a UITabBarController as RootViewController and a some subclass of UIView is one of the tabs that is shown when tapping on a TabBarItem.
furthermore i assume your UITextView and (UITextField?) is added to that UIView subclass and NOT the TabBarItem?
if that's the case, you could do a check using the text property:
if([yourTextView.text length] > 42) {
// truncate text; e.g. throw away last part or first part until the length is below 42
}
should be the same for a UITextField - just use the text property again

You should implement the UITextFieldDelegate and/or UITextViewDelegate methods,
textField:shouldChangeCharactersInRange:replacementString:
or
textView:shouldChangeTextInRange:replacementText:
respectively.
Set an instance of the class that implements those methods as the delegate for your view. The logic in the method should examine the incoming text and decide what to do based on the length.

I've found another similar way to implement this problem using Delegates:
#define MAX_LENGTH 20
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length >= MAX_LENGTH && range.length == 0)
{
return NO; // return NO to not change text
}
else
{return YES;}
}

Related

How to set UILabel's value according to UITextField as it is typed in iPhone?

I want to set UILabel according to the UITextField as it is typed. I mean if user want to type SAMPLE and he starts typing S then the lable should be set as S, then he types A label should also be A and so on. How to achieve this?
Please share suggestions.
Thanks in advance
Simplest way to do is to make a method and connect it with UiTextfield with event UIControlEventEditingChanged which will give you the trace on every character entered in the textfield.
[self.selectedTextField addTarget:self action:#selector(enterInLabel ) forControlEvents:UIControlEventEditingChanged];
-(void)enterInLabel
{
selectedLabel.text=selectedTextField.text;
}
The delegate also works for this as #brain said. The shouldChangeCharactersInRange: method can be a little confusing but the the following I think is pretty straight forward.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// myTextField delegate has been set
if ([textField isEqual:myTextField]){
NSMutableString *txt = [NSMutableString stringWithString:textField.text];
[txt replaceCharactersInRange:range withString:string]; //this is essentially how the textfield is updated after YES is returned
previewLabel.text = txt;
}
return YES;
}
The changing of the text field is actually done after the return YES. That is the whole point of this delegate. Just for an example, if you wanted to limit a textfield to 3 characters you could do the following to stop the text field from "replacing" the text.
if (range.location > 3)
return NO;
Set your textField delegate then call its method in your viewController.m file.. like this -
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
mylabel.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
This will change the text after each increment of character.
OR You can add action on your TextField
See this link - UiTextField events
Your view controller will need to implement the UITextFieldDelegate which will allow it to receive changes to the text field. In the appropriate methods of the delegate you need to set the text of the UILabel.
I have never used UITextFieldDelegate so can't provide more detail on how to use it. I would mock up a quick example and just NSLog or debug the delegate calls to see that calls you get.

How to determine which section header in UITableView is being edited

I have a UITextField in a custom section header. There are multiple sections using this style of header, and therefore multiple UITextFields.
I have implemented the UITextFieldDelegate. When I edit one of these UITextFields, it calls the delegate method textFieldDidEndEditing. How do I determine which section header this UITextField was in? I need to save the value to core data in the appropriate NSManagedObject for that section.
Many thanks in advance
EDIT: Several people have suggested using a tag of the section number when creating the cell, which would work perfectly. However, I have already assigned the UITextField a tag to distinguish it as a 'header' textfield as opposed to a cell textfield or a 'footer' textfield. There are a whole lotta textfields on this table!!
Further EDIT: Using in indexPath has been suggested. This would be my preferred solution if I can get it to work. Does anyone know if headers and footers have indexPaths?
You could use tags to identify UITextField instances. Since you're already setting tags in UITextField instances, set the tags on the section views itself:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *sectionView = ... // your section view instance
// assign the section index as the tag
sectionView.tag = section;
return sectionView;
}
In the textfield delegate, get the section index from the sender's parent:
- (void) textFieldDidEndEditing:(UITextField *)textField;
{
NSInteger theSectionIndex = textField.superview.tag;
// your custom logic here
}
You would probably want to look into UITableView method indexPathForCell:.
You can get the cell through view hierarchy from your UITextField, since it's in your Cell's contentView.
Regards,
sven.
It is very simple Mr.Ben Thompson. You have named different between each one to UITextFields am i right?. Just find the specific UITextField by used it's name.
-(void) textFieldDidEndEditing:(UITextField *)textField
{
if (textField == textfieldOne)
{
//Do whatever you want...
}
else if (textField == textfieldTwo)
{
//Do whatever you want...
}
else if (textField == textfieldThree)
{
//Do whatever you want...
}
}
I hope it will help you a little bit. Thanks.
i believe you add the field as a custom view to the header in viewForHeader method of tableview.
I suggest keeping a tag of the field using the section like this.
textfield.tag == section;
then in the delegate message you can have a switch method to compare tags..and do your own code there

Keypressed event in UITextField

I have 10 textfields, in which I could enter only one character in each textfield. After a character is entered in each textfield, the focus should move to the next one. Similarly when i delete character from a textfield by pressing the backspace or delete, i need to get the focus to the previous textfield. If I could get the keypressed event, I could do that. Right now I am not able to find any keypressed event examples.
Implement UITextFieldDelegate.
Implement the delegate methods in the protocol. You can achieve the things you wanted.
You can set the focus by using the method becomeFirstResponder to the required textfield.
Have a look at the delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
So that could solve your problem.
Based on Aadhira's answer, but taking into account Kirk Woll's comment, you can generate what the latest text will be by using stringByReplacingCharactersInRange:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"value: %#", value);
return YES;
}
Just to give you directions:
Assign tag to each text field.
Implement UITextFieldDelegate. There are all the methods you need to detect any event that takes place inside the text field. In each method you can check the tag and move focus properly.
Hint: you can use [mainView viewWithTag:XX] to quickly pick the text field you need.
Each time the text is changed you can check the text property of the text field and it will give you the answer which button was pressed.
you have to implement the UITextFieldDelegate protocol into your code and this method will tell you when you start begin editing in text field
– textFieldShouldBeginEditing:
and you can set the if condition in this method according to your requirement...
You have to use the textfieldDelegate methods.
In your textFieldShouldReturn method you have to set your responders like
if (textfield == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else
{
[textField3 resignFirstResponder];
}
return YES; // as method return type is BOOL.

iphone: uitextfield, multiple textfields with the same delegate?

there are two different textfields in my applications and i set the .delegate property of both of them to: self.
Now i implemented different methods from the uitextfielddelegate protocol but i want to be able to control the two textfields individually. For instance i want the first text field to do something different when editing begins than the second textfield... Is the only solution to this problem to set the assign a different delegate or is there a way to do this with both of the textfield having the same delegate assigned to them?
I hope my question is understandable i tried it to explain the best way that i could.....
thanks in advance!
Set a tag on the textfield on initialization, then check the UITextField object that is passed into the delegate method's tag, then you'll be able to make a differentiation between the two textfields:
#define FIELD_ONE_TAG 1
#define FIELD_TWO_TAG 2
UITextField *textFieldOne = ...
textFieldOne.tag = FIELD_ONE_TAG;
...
UITextField *textFieldTwo = ...
textFieldTwo.tag = FIELD_TWO_TAG;
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if(textField.tag == FIELD_ONE_TAG) { //field one
} else {//field two
}
}
UITextField *textFieldOne=.....
UITextField *textFieldTwo=....
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if(textField == textFieldOne)
{ // field one code
}else{
//field two code
}
}
have two references of the inserted text views and u can compare them at the delegate methods. Not much needed with tags

Display the iPhone keyboard

I've run into a problem with the UITextView that seems to be related to having a scrollable view within a scrollable view.
In order to remedy this i thought i would attempt to write my own multiline (but not scrollable) text view. Given the core graphics methods, and the UITextInputTraits class it seems like this should be feasable. The only thing i cant figure out is wether or not its possible to display (and catch events) for the system wide keyboard.
Is this even possible using the SDK?
What I did in a similar situation, is made a hidden UITextField, and set its delegate to your class where you can implement the appropriate UITextFieldDelegate methods to intercept the key's pressed.
something like this:
UITextField *myHiddenTextField = [[UITextField alloc] initWithFrame: cgRectZero()];
myHiddenTextField.delegate = self;
[myHiddenTextField becomeFirstResponder];
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//use string here for the text input
return false;
}