How do I create delegates in Objective-C?

后端 未结 19 2510
一整个雨季
一整个雨季 2020-11-21 04:48

I know how delegates work, and I know how I can use them.

But how do I create them?

相关标签:
19条回答
  • 2020-11-21 05:13

    In my point of view create separate class for that delegate method and you can use where you want.

    in my Custom DropDownClass.h

    typedef enum
    {
     DDSTATE,
     DDCITY
    }DropDownType;
    
    @protocol DropDownListDelegate <NSObject>
    @required
    - (void)dropDownDidSelectItemWithString:(NSString*)itemString     DropDownType:(DropDownType)dropDownType;
    @end
    @interface DropDownViewController : UIViewController
    {
     BOOL isFiltered;
    }
    @property (nonatomic, assign) DropDownType dropDownType;
    @property (weak) id <DropDownListDelegate> delegate;
    @property (strong, nonatomic) NSMutableArray *array1DropDown;
    @property (strong, nonatomic) NSMutableArray *array2DropDown;
    

    after that in.m file create array with objects,

     - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat rowHeight = 44.0f;
    return rowHeight;
    }
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return isFiltered?[self.array1DropDown count]:[self.array2DropDown count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"TableCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    if (self.delegate) {
        if (self.dropDownType == DDCITY) {
            cell.textLabel.text = [self.array1DropDown objectAtIndex:indexPath.row];
        }
        else if (self.dropDownType == DDSTATE) {
            cell.textLabel.text = [self.array2DropDown objectAtIndex:indexPath.row];
        }
    }
    return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
     [self dismissViewControllerAnimated:YES completion:^{
        if(self.delegate){
            if(self.dropDownType == DDCITY){
                [self.delegate dropDownDidSelectItemWithString:[self.array1DropDown objectAtIndex:indexPath.row] DropDownType:self.dropDownType];
            }
            else if (self.dropDownType == DDSTATE) {
                [self.delegate dropDownDidSelectItemWithString:[self.array2DropDown objectAtIndex:indexPath.row] DropDownType:self.dropDownType];
            }
        }
    }];
    }
    

    Here all are set for Custom delegate class.after that you can use this delegate method where you want.for example...

    in my another viewcontroller import after that

    create action for calling delegate method like this

    - (IBAction)dropDownBtn1Action:(id)sender {
    DropDownViewController *vehicleModelDropView = [[DropDownViewController alloc]init];
    vehicleModelDropView.dropDownType = DDCITY;
    vehicleModelDropView.delegate = self;
    [self presentViewController:vehicleModelDropView animated:YES completion:nil];
    }
    

    after that call delegate method like this

    - (void)dropDownDidSelectItemWithString:(NSString *)itemString DropDownType:(DropDownType)dropDownType {
    switch (dropDownType) {
        case DDCITY:{
            if(itemString.length > 0){
                //Here i am printing the selected row
                [self.dropDownBtn1 setTitle:itemString forState:UIControlStateNormal];
            }
        }
            break;
        case DDSTATE: {
            //Here i am printing the selected row
            [self.dropDownBtn2 setTitle:itemString forState:UIControlStateNormal];
        }
    
        default:
            break;
    }
    }
    
    0 讨论(0)
  • 2020-11-21 05:16

    Answer is actually answered, but I would like to give you a "cheat sheet" for creating a delegate:

    DELEGATE SCRIPT
    
    CLASS A - Where delegate is calling function
    
    @protocol <#Protocol Name#> <NSObject>
    
    -(void)delegateMethod;
    
    @end
    
    @interface <#Some ViewController#> : <#UIViewController#> 
    
    @property (nonatomic, assign) id <<#Protocol Name#>> delegate;
    
    @end
    
    
    @implementation <#Some ViewController#> 
    
    -(void)someMethod {
        [self.delegate methodName];
    }
    
    @end
    
    
    
    
    CLASS B - Where delegate is called 
    
    @interface <#Other ViewController#> (<#Delegate Name#>) {}
    @end
    
    @implementation <#Other ViewController#> 
    
    -(void)otherMethod {
        CLASSA *classA = [[CLASSA alloc] init];
    
        [classA setDelegate:self];
    }
    
    -delegateMethod() {
    
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-21 05:21

    Swift version

    A delegate is just a class that does some work for another class. Read the following code for a somewhat silly (but hopefully enlightening) Playground example that shows how this is done in Swift.

    // A protocol is just a list of methods (and/or properties) that must
    // be used by any class that adopts the protocol.
    protocol OlderSiblingDelegate: class {
        // This protocol only defines one required method
        func getYourNiceOlderSiblingAGlassOfWater() -> String
    }
    
    class BossyBigBrother {
        
        // The delegate is the BossyBigBrother's slave. This position can 
        // be assigned later to whoever is available (and conforms to the 
        // protocol).
        weak var delegate: OlderSiblingDelegate?
        
        func tellSomebodyToGetMeSomeWater() -> String? {
            // The delegate is optional because there might not be anyone
            // nearby to boss around.
            return delegate?.getYourNiceOlderSiblingAGlassOfWater()
        }
    }
    
    // PoorLittleSister conforms to the OlderSiblingDelegate protocol
    class PoorLittleSister: OlderSiblingDelegate {
    
        // This method is repquired by the protocol, but the protocol said
        // nothing about how it needs to be implemented.
        func getYourNiceOlderSiblingAGlassOfWater() -> String {
            return "Go get it yourself!"
        }
        
    }
    
    // initialize the classes
    let bigBro = BossyBigBrother()
    let lilSis = PoorLittleSister()
    
    // Set the delegate 
    // bigBro could boss around anyone who conforms to the 
    // OlderSiblingDelegate protocol, but since lilSis is here, 
    // she is the unlucky choice.
    bigBro.delegate = lilSis
    
    // Because the delegate is set, there is a class to do bigBro's work for him.
    // bigBro tells lilSis to get him some water.
    if let replyFromLilSis = bigBro.tellSomebodyToGetMeSomeWater() {
        print(replyFromLilSis) // "Go get it yourself!"
    }
    

    In actual practice, delegates are often used in the following situations

    1. When a class needs to communicate some information to another class
    2. When a class wants to allow another class to customize it

    The classes don't need to know anything about each other beforehand except that the delegate class conforms to the required protocol.

    I highly recommend reading the following two articles. They helped me understand delegates even better than the documentation did.

    • What is Delegation? – A Swift Developer’s Guide
    • How Delegation Works – A Swift Developer’s Guide
    0 讨论(0)
  • 2020-11-21 05:21

    Let's start with an example , if we buy a product online ,it goes through process like shipping/delivery handled by different teams.So if shipping gets completed ,shipping team should notify delivery team & it should be one to one communication as broadcasting this information would be overhead for other people / vendor might want to pass this information only to required people.

    So if we think in terms of our app, an event can be an online order & different teams can be like multiple views.

    Here is code consider ShippingView as Shipping team & DeliveryView as delivery team :

    //Declare the protocol with functions having info which needs to be communicated
    protocol ShippingDelegate : class {
        func productShipped(productID : String)
    }
    //shippingView which shows shipping status of products
    class ShippingView : UIView
    {
    
        weak var delegate:ShippingDelegate?
        var productID : String
    
        @IBAction func checkShippingStatus(sender: UIButton)
        {
            // if product is shipped
            delegate?.productShipped(productID: productID)
        }
    }
    //Delivery view which shows delivery status & tracking info
    class DeliveryView: UIView,ShippingDelegate
    {
        func productShipped(productID : String)
        {
            // update status on view & perform delivery
        }
    }
    
    //Main page on app which has both views & shows updated info on product whole status
    class ProductViewController : UIViewController
    {
        var shippingView : ShippingView
        var deliveryView : DeliveryView
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // as we want to update shipping info on delivery view, so assign delegate to delivery object
            // whenever shipping status gets updated it will call productShipped method in DeliveryView & update UI.
            shippingView.delegate = deliveryView
            //
        }
    }
    
    0 讨论(0)
  • 2020-11-21 05:22

    I think all these answers make a lot of sense once you understand delegates. Personally I came from the land of C/C++ and before that procedural languages like Fortran etc so here is my 2 min take on finding similar analogues in C++ paradigm.

    If I were to explain delegates to a C++/Java programmer I would say

    What are delegates ? These are static pointers to classes within another class. Once you assign a pointer, you can call functions/methods in that class. Hence some functions of your class are "delegated" (In C++ world - pointer to by a class object pointer) to another class.

    What are protocols ? Conceptually it serves as similar purpose as to the header file of the class you are assigning as a delegate class. A protocol is a explicit way of defining what methods needs to be implemented in the class who's pointer was set as a delegate within a class.

    How can I do something similar in C++? If you tried to do this in C++, you would by defining pointers to classes (objects) in the class definition and then wiring them up to other classes that will provide additional functions as delegates to your base class. But this wiring needs to be maitained within the code and will be clumsy and error prone. Objective C just assumes that programmers are not best at maintaining this decipline and provides compiler restrictions to enforce a clean implementation.

    0 讨论(0)
  • 2020-11-21 05:22

    lets say you have a class that you developed and want to declare a delegate property to be able to notify it when some event happens :

    @class myClass;
    
    @protocol myClassDelegate <NSObject>
    
    -(void)myClass:(MyClass*)myObject requiredEventHandlerWithParameter:(ParamType*)param;
    
    @optional
    -(void)myClass:(MyClass*)myObject optionalEventHandlerWithParameter:(ParamType*)param;
    
    @end
    
    
    @interface MyClass : NSObject
    
    @property(nonatomic,weak)id< MyClassDelegate> delegate;
    
    @end
    

    so you declare a protocol in MyClass header file (or a separate header file) , and declare the required/optional event handlers that your delegate must/should implement , then declare a property in MyClass of type (id< MyClassDelegate>) which means any objective c class that conforms to the protocol MyClassDelegate , you'll notice that the delegate property is declared as weak , this is very important to prevent retain cycle (most often the delegate retains the MyClass instance so if you declared the delegate as retain, both of them will retain each other and neither of them will ever be released).

    you will notice also that the protocol methods passes the MyClass instance to the delegate as parameter , this is best practice in case the delegate want to call some methods on MyClass instance and also helps when the delegate declares itself as MyClassDelegate to multiple MyClass instances , like when you have multiple UITableView's instances in your ViewController and declares itself as a UITableViewDelegate to all of them.

    and inside your MyClass you notify the delegate with declared events as follows :

    if([_delegate respondsToSelector:@selector(myClass: requiredEventHandlerWithParameter:)])
    {
         [_delegate myClass:self requiredEventHandlerWithParameter:(ParamType*)param];
    }
    

    you first check if your delegate responds to the protocol method that you are about to call in case the delegate doesn't implement it and the app will crash then (even if the protocol method is required).

    0 讨论(0)
提交回复
热议问题