HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS协议/代表混淆?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这一切都是我的第一篇文章,我将尽可能准确.我已经阅读了很多关于iOS协议/委托实现的文章,但所有示例都失败了.
让我们说吧
我有A和B控制器,想要从A到B发送数据.

@protocol exampleprot <NSObject>
@required
-(void) exampledmethod:(NSString *) e1;
@end

@interface ViewController
{
__weak id <exampleprot> delegate
}


上午
在某些程序中
我试着推

[delegate  examplemethod:@"test"]

B.h

@interface test2 : UiViewcontroller <exampleprot>

在B.m实现方法 – (void)exampledmethod:(NSString *)e1;

那么我做错了什么?

解决方法

基本上这是自定义委托的示例,它用于将消息从一个类发送到另一个类.因此,要在另一个类中发送消息,您需要先设置委托,然后在另一个类中使用协议.以下是示例:

班级

@protocol sampleDelegate <NSObject>
@required
-(NSString *)getDataValue;
@end
@interface BWindowController : NSWindowController
{
    id<sampleDelegate>delegate;
}
@property(nonatomic,assign)id<sampleDelegate>delegate;
@end

在B.m班

- (void)windowDidLoad
{
 //below only calling the method but it is impelmented in AwindowController class
   if([[self delegate]respondsToSELEctor:@SELEctor(getDatavalue)]){
    NSString *str= [[self delegate]getDataValue];
     NSLog(@"Recieved=%@",str);
    }
    [super windowDidLoad];
}

在A.h班

@interface AWindowController : NSWindowController<sampleDelegate> //conforming to the protocol

在A.m班

//ImplemenTing the protocol method 
    -(NSString*)getDataValue
    {
        NSLog(@"recieved");
        return @"recieved";
    }
//In this method setTing delegate AWindowController to BWindowController
    -(void)yourmethod
    {

    BWindowController *b=[[BWindowController alloc]init];
    b.delegate=self;   //here setTing the delegate 

    }

大佬总结

以上是大佬教程为你收集整理的iOS协议/代表混淆?全部内容,希望文章能够帮你解决iOS协议/代表混淆?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。