iOS协议/代理混淆?

iOS协议/代理混淆?,ios,objective-c,Ios,Objective C,所有这些都是我的第一篇文章,我会尽可能的精确。我读过很多关于iOS协议/委托实现的文章,但所有的例子都失败了。 比方说 我有A和B控制器,想从A向B发送数据。 A.h B.h @接口测试2:UiViewcontroller 在B.m实现方法中-(void)exampledmethod:(NSString*)e1 那么我做错了什么呢?基本上这是自定义委托的示例,用于从一个类向另一个类发送消息。因此,要在另一个类中发送消息,您需要首先设置委托,然后在另一个类中遵循协议。下面是一个例子:- B.

所有这些都是我的第一篇文章,我会尽可能的精确。我读过很多关于iOS协议/委托实现的文章,但所有的例子都失败了。 比方说 我有A和B控制器,想从A向B发送数据。 A.h


B.h

@接口测试2:UiViewcontroller
在B.m实现方法中-(void)exampledmethod:(NSString*)e1



那么我做错了什么呢?

基本上这是自定义委托的示例,用于从一个类向另一个类发送消息。因此,要在另一个类中发送消息,您需要首先设置委托,然后在另一个类中遵循协议。下面是一个例子:-

B.h
class

@protocol sampleDelegate <NSObject>
@required
-(NSString *)getDataValue;
@end
@interface BWindowController : NSWindowController
{
    id<sampleDelegate>delegate;
}
@property(nonatomic,assign)id<sampleDelegate>delegate;
@end
- (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];
}
@interface AWindowController : NSWindowController<sampleDelegate> //conforming to the protocol
 //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 

    }
A.h
类中

@protocol sampleDelegate <NSObject>
@required
-(NSString *)getDataValue;
@end
@interface BWindowController : NSWindowController
{
    id<sampleDelegate>delegate;
}
@property(nonatomic,assign)id<sampleDelegate>delegate;
@end
- (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];
}
@interface AWindowController : NSWindowController<sampleDelegate> //conforming to the protocol
 //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 

    }

如果要将数据从A发送到B,则应在B中定义一个返回类型为的委托。在B中,将有代表声明:

@protocol example <NSObject>
@required
-(NSString *) exampleMethod;
@end
然后在B中,你所需要做的就是使用该委托从A中获得你想要的。在B.m中,你有

- (void)methodInB {
    // Get string from delegate
    self.string = [self.delegate exampleMethod];
}

您需要设置视图控制器的委托属性,从该属性发送数据=视图控制器发送数据。我认为侯赛因的回答是正确的。您只需检查您的操作方式是否正确。

您的任务是什么…?您是否分配了代表?比如
objectOfA.delegate=objectOfB
?如果不是属性,还有其他方法吗?我想通过协议/委托将数据从a发送到B(通过示例方法传递字符串)控制器,但我错过了一些步骤,在网上阅读了很多o文章后,我不明白我做错了什么?你的解释很好,我混合了协议的放置位置,如果发送A到B-那么协议应该在B-谢谢,其他的事情,我尝试了你的例子,但仍然接收=空?你能给我看一下你的代码吗??您在其中设置委托??我在您的操作方法或其他方法中的A.mSet的viewDidLoad中添加它,尽管在load方法中设置,我尝试过,但它是相同的,始终为NULL?你有什么建议?