iOS Development-Delegation (Delegate) talking about

Source: Internet
Author: User

In fact, the delegation is not in OC, C # also has, but each other's understanding is not the same, OC in the delegation is a protocol, need to use the @protocol declaration, delegates generally in the iOS development page in the use of more than the value. A delegate is one of the simplest and most flexible patterns in cocoa, and the delegate literally means delegating what is needed to others, and the business scenario can refer to the page relationship between the main view and the child views, or the interaction between the view layer and the data layer.

a simple delegate

Delegates through @protocol declaration, you can define a method, reference the object of the delegate, you need to implement its methods, the method is @required by default, and can be set to optional @optional, first define a delegate:

@protocol bookdelegate <NSObject> @required-(void) Getbookcount; @optional-(void) Optionmethod; @end

This time defines the books book class and the Client customer class:

@interface book:nsobject<bookdelegate> @end @interface customer:nsobject<bookdelegate> @property (assign , nonatomic) id<bookdelegate> didbookdelegate; @end

To implement the Getbookcount method:

@implementation book-(void) getbookcount{    NSLog (@ "The implementation of Getbookcount in Book");} @end @implementation customer-(void) getbookcount{    NSLog (@ "Implementation of Getbookcount in Customer");} @end

Simple invocation:

    Book *book=[[book Alloc]init];        Customer *customer=[[customer Alloc]init];        [Customer Getbookcount];        [Book Getbookcount];

The results of the above few lines of code, needless to say that everyone can read, then look at the next code, this time we found the use of the beginning of the top one didbookdelegate:

     Customer.didbookdelegate=book;        [Customer.didbookdelegate Getbookcount];

The above is the book instance, book implementation of the Bookdelegate, this time can be assigned to the book instance of the variables in the customer, the instantiation of their own object to the Didbookdelegate.

The above is a delegate using a basic scenario, as an instantiated object book can execute its own method, or the execution process can be transferred through a delegate.

Page Pass Value

Simple is a page data can be passed to the B page, b page can be transmitted to a page, a simple two page value, page reference as follows:

are both text boxes and buttons, the jump mode is selected when modal:

The definition of the first page Viewcontroller:

#import <UIKit/UIKit.h> #import "SecondViewController.h" @interface viewcontroller:uiviewcontroller< Studysubjectdelegate> @property (Strong, nonatomic) Iboutlet nsstring *firstdata; @property (weak, nonatomic) Iboutlet Uitextfield *subjectname; @end

The second page Secondviewcontroller the definition of the header file and declares a delegate:

  secondviewcontroller.h//  sample////  Created by Keso on 15/2/3.//  Copyright (c) 2015 Keso. All rights reserved.//#import <UIKit/UIKit.h> @class secondviewcontroller @protocol studysubjectdelegate < nsobject>-(void) Shouldchangevalue: (secondviewcontroller*) controller; @end @interface Secondviewcontroller: Uiviewcontroller@property (assign,nonatomic) id<studysubjectdelegate> firstviewdelegate; @property (Weak, nonatomic) Iboutlet nsstring *showdata; @property (weak, nonatomic) Iboutlet Uitextfield *studysubject; @end

Click events in the VIEWCONTROLLER.M:

-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender{    if ([Segue.identifier isequaltostring:@] Firstedit "]) {        Secondviewcontroller *controller=segue.destinationviewcontroller;        NSLog (@ "%@", self.subjectName.text);        Passing an instance of itself to the second view        controller.firstviewdelegate=self;        Controller.showdata=self.subjectname.text;}    }

The way to assign the value can also be, in fact, the key is the second view defines the properties:

        if ([Controller respondstoselector: @selector (setshowdata:)]) {            [controller SetValue:self.subjectName.text forkey:@ "ShowData"];        }

The second page of the Click event is relatively simple, the code is as follows:

    [Self.firstviewdelegate shouldchangevalue:self];

In fact, the above can be simply seen in OC is the delegate to the other object's member variables, and then by its member variables to perform the work of the instance, today do not know why a little headache, saying that the last second page to jump to a page can receive the value, Can not give Uitextfield assignment, temporarily do not understand how to assign value up, each entry becomes null, have know can point, thank you ~

iOS Development-Delegation (Delegate) talking about

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.