问题描述
我有两个视图控制器VC1和VC2.我通过prepareForSegue方法将值从VC1传递到VC2.当我在VC2中收到该值时,将其乘以10.然后在VC2中将该值乘以10后,我想将其返回给VC1.
I have two view controllers VC1 and VC2. I pass a value from VC1 to VC2 through prepareForSegue method. and when I receive that value in VC2 I multiply it by 10. Then after multiplying that value by 10 in VC2 I want to return it back to VC1.
要解决此问题,我将使用 Delegate
.因此,我创建了 DelegateDataProcessor
类
to solve this issue, I would use Delegate
. so, I created DelegateDataProcessor
class
但是我不知道如何在方法 startProcessingValue
but I do not know how to call the required method processingDone
in the method startProcessingValue
请看下面的代码
** DelegateDataProcessor.h **:
** DelegateDataProcessor.h**:
#import <Foundation/Foundation.h>
@protocol ProtocolDataProcessor <NSObject>
@required
- (void) processingDone;
@end
@interface DelegateDataProcessor : NSObject{
id <ProtocolDataProcessor> _delegate;
}
@property (nonatomic, strong) id delegate;
-(void) startProcessingValue: (NSInteger) valueToBeProcessed;
@end
** DelegateDataProcessor.m **:
** DelegateDataProcessor.m**:
#import "DelegateDataProcessor.h"
@implementation DelegateDataProcessor
-(void) startProcessing:(NSInteger) value {
value = value * 10;
//How to call the method `processingDone` here
}
@end
推荐答案
您可以通过协议将数据从VC2传递到VC1:
You can pass your data from VC2 to VC1 via a protocol:
-
向协议添加方法:
Add a method to protocol:
-(void)getValue:(NSInteger)myInteger;
在准备segue的方法上,将VC1定义为VC2的委托
On method prepare for segue, define VC1 as VC2's delegate
当您希望从VC2返回到VC1时,可以通过传递值来调用方法getValue.
When you want come back from VC2 to VC1 call method getValue by passing your value.
我希望我能清楚地解释.
I wish that I explained clearly.
这篇关于如何在实现文件中的委托中调用所需的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!