Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Delegates in iOS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 488
    Comment on it

    Means of communication between  the objects can be implemented using Delegates. One object can send messages to another object through delegates.
    For example, if we want to pass data forward then it is easy through UINavigationController as we can just use segue. And when we want to pass the data backwards then for that we can use delegates for sending data backwards easily.

    Lets take an example which shows the use of delegate. In this example we will create two ViewControllers. We will enter first name in one controller and last name in another and then we will show the full name in the first ViewController on the Tap of the button :-

    First of all create a new project in Xcode. Now in your Main.Storyboard. Drag two UILabels , two UITextfields and a UIButton in the ViewControllor.
    Take another ViewController from the object library and drag a UILabel, a UITextField and a UIButton on it. Embed the ViewController into UINavigationController.

    Now create a New file which is subclass of UIViewController and assign its name NextViewController(here). Assign this class to the second ViewController in the Main.Storyboard.

    In your NextViewController.h implement the following code :

    @protocol NextViewControllerDelegate   // create a protocol
    -(void)setLastName:(NSString *)lastName;   // declare its methods
    @end

    Create the IBOutlets of the dragged UITextfield and UIButton and connect it :-

    @interface NextViewController : UIViewController
    
    @property (nonatomic,retain) id delegate;   // delegate property
    
    - (IBAction)btnDone:(id)sender;     // action on button 
    
    @property (weak, nonatomic) IBOutlet UITextField *txtLastname;  // outlet on textfield
    
    @end

    Now, in your NextViewController.m file perform the action of UIButton :-

    - (IBAction)btnDone:(id)sender {
        
        [_txtLastname resignFirstResponder];
        [self.delegate setLastName:_txtLastname.text];  // we set the value of last name to the delegate
        [self.navigationController popViewControllerAnimated:YES];
        
    }

    Now in the ViewController class we have to get the name which we have entered in the TextField of NextViewController. In the ViewController.h file create the IBOutlets of textFields and IBAction of the UIButton as :-

    @property (weak, nonatomic) IBOutlet UITextField *txtFirstName;  // outlet on textfield to enter first name
    @property (weak, nonatomic) IBOutlet UITextField *txtFullName;   // outlet on textfield to get full name
    
    - (IBAction)btnGetLastName:(id)sender;  //  action on button to move to next controller
    

    Now in your ViewController.m file implement the following code :-

    -(void)setLastName:(NSString *)lastName  // get the value of last name, implement the delegate method declared in the protocol
    {
        NSString *fullName = [NSString stringWithFormat:@"%@ %@",_txtFirstName.text,lastName];
        _txtFullName.text = fullName;  // set the full name
    }
    
    
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        NextViewController *nextViewController = [segue destinationViewController];
        nextViewController.delegate = self;       // set the delegate to itself as we are loading the controller through segue
    }

    So, in this example we can see the last name is entered in second controller and with the help of delegate we fetched it back in the initial controller.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: