How do I create delegates in Objective-C?

后端 未结 19 2612
一整个雨季
一整个雨季 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: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#> 
    
    -(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
    

提交回复
热议问题