I hope you know a little bit about iOS programming. By considering it I will tell you the concept of UITextField and its Delegate Methods. Visit the best iOS App development company
UITextField is one of the basic components used widely. UITextfield is used to get some editable text or integer input from the user. This input is displayed or used for processing.
We should know What is Delegate? First.
I will share a very good example with you -
The manager assigns a work to an employee. Now when the work is finished Manager should know the status of the work as finished. This methodology is called as ‘delegate’ in iOS.
Lets Start with an Example,
Here we will implement the ‘UITextfield’ and its delegate methods in your iOS application. Now I am going to create a Tutorial, which displays the input entered in the UI TextFied when the user clicks on the UIButton.
Create a new empty application in Xcode. Name this application as ‘UITextfield Demo‘. Create a new file like I did in my previous tutorials File–>New–>File. Select the file type as Objective c. Name this file as ‘HomeViewController‘. Ok now we have the required files to work on.
Design Part -
Now select the file ‘HomeViewController.xib‘. Drag and drop an UIButton and UITextField. After this your view may look similar to this picture below.
Select the UITextField and set IBOutlet. Give the name to the UITextField as ‘textField1‘. After creating an IBOutlet successfully, now I need to set the delegate to it.
Select the UITextField in the view. Drag the delegate field to the file-owner and connect the blue line. Now I have set the UITextField delegate successfully.
Now select the UIButton and create an IBAction. Give a name to the IBAction.Here I have given a name as ‘displayAction‘.
Coding Part -
As I finished the design part its time to make some code work. Select the file ‘HomeViewController.h‘. Add these lines of code as shown in the picture below.
- This code declares a UIAlertView.It is used to display a simple alert with a message.
1 | @property ( nonatomic ,retain)UIAlertView*alertView; |
Whenever you set a property, It is recommended to set the ‘@synthesize‘ to that relevant property element or variable. This ‘property’ and ‘@synthesize’ is used to create getter and setter methods. So here I am having two properties namely textField1 and alertView. Now set ‘@synthesize‘ to these elements in the ‘HomeViewController.m‘ file.
[Note - 'property' and '@synthesize' is used to create getter and setter methods for the UIComponents or variables]
Add this line in the ‘HomeViewController.m’ below the @implementation part,
1 | @synthesize alertView, textField1; |

Next I am going to add some UITextField delegate methods to handle the UITextField processing. Oh I forget to tell you what they are. UITextField has several types of delegate methods. Some of them are,
- textFieldShouldBeginEditing – Method gets called when the textfield editing will start
- textFieldDidBeginEditing - Method gets called when the textfield editing started already
- textFieldShouldEndEditing - Method gets called when the textfield editing will end
- textFieldDidEndEditing - Method gets called when the textfield editing ended already
- textFieldshouldChangeCharactersInRange – Method gets called when typing every single character (You can restrict or modify the character while typing using this method)
- textFieldShouldReturn - Method gets called when the keyboard return key pressed
Now add these lines of code inside the ‘HomeViewController.m‘ file. Here I have implemented the delegate methods textFieldDidBeginEditing,textFieldDidEndEditing and textFieldShouldReturn. I have displayed an alert when the delegate method gets called and when the UIButton clicked.
Add this code inside ‘HomeViewController.m’ file:-
1 | - ( IBAction ) displayAction : ( id ) sender { |
3 | if (textField1.text.lenght == 0) { |
5 | alertView = [[UIAlertView alloc] initWithTitle : [ NSString stringWithFormat : @"%@" , textField1.text] message : nil delegate : self cancelButtonTitle : @"Ok" otherButtonTitles: nil ]; |
9 | alertView = [[UIAlertView alloc] initWithTitle : [ NSString stringWithFormat : @"TextField is Empty!" ] message : nil delegate : self cancelButtonTitle : @"Ok" otherButtonTitles: nil ]; |
17 | # pragma mark - TextField Delegate Methods |
19 | - ( void ) textFieldDidBeginEditing : (UITextField *) textField { |
21 | alertView = [[UIAlertView alloc] initWithTitle : [ NSString stringWithFormat : @"TextField Begin Editing Method Called" ] message : nil delegate : self cancelButtonTitle : @"Ok" otherButtonTitles: nil ]; |
27 | - ( void ) textFieldDidEndEditing : (UITextField *) textField { |
29 | alertView = [[UIAlertView alloc] initWithTitle : [ NSString stringWithFormat : @"TextField End Editing Method Called" ] message : nil delegate : self cancelButtonTitle : @"Ok" otherButtonTitles: nil ]; |
35 | - ( BOOL ) textFieldShouldReturn : (UITextField *) textField { |
37 | alertView = [[UIAlertView alloc] initWithTitle : [ NSString stringWithFormat : @"TextField Should Return Method Called" ] message : nil delegate : self cancelButtonTitle : @"Ok" otherButtonTitles: nil ]; |
41 | [textField resignFirstResponder]; |
Now this is going to be the last and important step you need to do. Set the HomeViewController as root View Controller in the method named " application "didFinishLaunchingWithOptions’ in the file ‘AppDelegate.m’. If you are not sure about that,
- Import the file ‘HomeViewController.h’ in AppDelegate.m
1 | #import "HomeViewController.h" |
- Add this lines of code inside the method applicationdidFinishLaunching
1 | HomeViewController *homeViewController = [[HomeViewController alloc] init]; |
2 | self .window.rootVIewController = homeViewController; |
This is how out UITextField will work in iOS.
Thank you..!!!
No comments:
Post a Comment