想象一下,你有一个名为A的UITableView。
在A中,您实现了一个委托方法来打印通过名为B的详细视图控制器中定义的协议传递给它的数据。
在A的A.m实现文件中,您有以下代码:
#import "A.h"
#import "B.h"
@interface A()
@end
@implementation
//bunch of methods not relevant to this question first
//Implement method that passes myStringData sent back from B to A
#pragma mark - Send data back to cell tapped in feed
-(void)sendDataBackToA:(NSString *)myStringData
{
if ([myStringData rangeOfString:@"Ciao_Bello"].location == NSNotFound)
{
NSLog(@"The string delegate back to A in case 1 is %@", myStringData);
}
else
{
NSLog(@"The string delegate back to A in case 2 is %@", myStringData);
}
}
在B.h标题中,您声明相关协议和委托:
@protocol senddataProtocol <NSObject>
-(void)sendDataBackToA:(NSString)myStringData
@end
@interface B: UIViewController<UIPopoverPresentationControllerDelegate, UIAdaptivePresentationControllerDelegate>
@property(nonatomic, assign)id<senddataProtocol> stringDelegate;
@end
实现B.m时,确保合成您的委托属性并在viewWillDisappear中调用委托方法,打印您传递的NSString:
@implementation B
@synthesize stringDelegate;
//A few methods' implementations not relevant to this questions first
//Call delegate method to pass data from B to A in viewWillDisappear
-(void)viewWillDisappear:(BOOL)animated
{
if ([self.button.titleLabel.text rangeOfString:@"text1"].location == NSNotFound)
{
[stringDelegate sendDataBackToA:@"Ciao_Bello"];
NSLog(@"The string delegate sent from B to A is %@", @"Ciao_Bello");
}
else
{
[[stringDelegate sendDataBackToA:@"Forza_Italia"];
NSLog(@"The string delegate sent from B to A is %@", @"Forza_Italia");
}
}
然后你运行你的构建。
根据为B中的按钮设置的NSString值(self.button.titleLabel.text),您会看到@“Ciao_Bello”和@“Forza_Italia”在您的日志中正确打印,这取决于为B中的按钮设置的NSString值。
但是你也在你的日志中看到,从未在A中调用sendDataBackToA委托方法。
你需要做些什么来解决这个问题?
谢谢!