简体   繁体   English

自定义委托没有被调用?

[英]Custom delegate is not getting called?

Actually I have three view controllers named in my app. 实际上,我在应用程序中命名了三个视图控制器。 I am navigating from A to B and then B to C. I am calling delegate method from C which is implemented in A. here is my code for 我从A导航到B,然后从B导航到C。我从C调用委托方法,该方法在A中实现。这是我的代码

Ah

#import "A.h"
#import "C.h"

In Am I have Am我有

@interface A()<delegateName>
-(void)delegateMethod
{
  NSLog(@"delegate");
}
-(void)moveToB
{
  C *instanceOfC=[C alloc] init];
  instanceOfC.delegate=self;                //line 1
}

In Bh Bh

#"import C.h"

In Bm Bm

-(void)moveToC
{
  A *instanceOfA=[[A alloc] init];
  C *instanceOfC=[[C alloc] init];      
  instanceOfC.delegate= instanceOfA;            //line 2
}

In Ch Ch

@protocal delegateName <NSObject>
-(void)delegateMethod;
@end
@interface C
@property(nonatomic,weak) id<delegateName>  delegate;
@end

In Cm Cm

@synthesize delegate;
-(void)inSomeMethod
{
 [delegate delegateMethod];
}

if I put <delegateName> in Ah instead of Am then it says undeclared delegate . 如果我将<delegateName>放在Ah而不是Am则它表示undeclared delegate

In moveToC you are assigning instanceOfA to the delegate property of instanceOfC . moveToC您要分配instanceOfA到的委托财产instanceOfC At the end of moveToC both instances are deallocated, as you do not keep a strong reference to instanceOfC , eg assign it to a property. moveToC结束时,两个实例都被释放,因为您没有对instanceOfC保持强引用, instanceOfC ,将其分配给属性。

You can fix this by adding a @property (strong, nonatomic) C *cInstance; 您可以通过添加@property (strong, nonatomic) C *cInstance;来解决此@property (strong, nonatomic) C *cInstance; to your Bm class and assign it when instantiating instanceOfC : 到您的Bm类,并在实例化instanceOfC时进行分配:

-(void)moveToC {
    A *instanceOfA=[[A alloc] init];
    self.cInstance = [[C alloc] init];
    self.cInstance.delegate= instanceOfA;
}

That said, the design looks wrong to me. 就是说,设计对我来说似乎是错误的。 I would rename delegateProtocol to somthing else, like DirtyJobHandler and introduce a custom init method in class C : 我将DirtyJobHandler类的东西重命名为delegateProtocol DirtyJobHandler并在类C引入了自定义init方法:

- (instanceType)initWithDirtyJobHandler:id<DirtyJobHandler> dirtyJobHandler {
    self = [super init];
    if (self) {
        self.dirtyJobHandler = dirtyJobHandler //previous knonw as self.delegate
    }
}

which is able to inject this dependency: 能够注入这种依赖性:

[[C alloc] initWithDirtyJobHandler:instanceOfA];

I think problem is in the line 我认为问题就在这里

A *instanceOfA=[[A alloc] init]; 

in the function 在功能上

-(void)moveToC
{
 A *instanceOfA=[[A alloc] init];
 C *instanceOfC=[[C alloc] init];
 instanceOfC.delegate= instanceOfA;
 }

Replace the first line with 将第一行替换为

  A *instanceofA = self.navigationController.viewControllers[indexofA]

In B viewcontroller you are again allocate new memory for A viewcontroller and set the delegate to it. 在B viewcontroller中,您再次为A viewcontroller分配新的内存并将委托设置给它。 Instead of that you need to set the delegate which is previously in the memory and that can be find in two ways 取而代之的是,您需要设置以前在内存中的委托,可以通过两种方式找到该委托

(i) Get it from navigation controller (ii) Create a object of A viewcontroller in B viewcontroller. (i)从导航控制器获取它(ii)在B viewcontroller中创建A viewcontroller的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM