【问题标题】:custom deleget not responding ToSelector自定义删除没有响应 ToSelector
【发布时间】:2016-01-03 06:18:21
【问题描述】:

以下是我的代码,没有错误但选择器没有响应。

ExampleTableviewSubProductDetail.h

中的代码
@protocol EnterAmountDelegate <NSObject>

 -(void)titlechange:(NSInteger)amount;

@end

@class ASIFormDataRequest;
@interface ExampleTableviewSubProductDetail :  UIViewController<UIScrollViewDelegate>
{
}
@property (nonatomic, strong) id <EnterAmountDelegate>delegate;

ExampleTableviewSubProductDetail.m

中的代码
  @implementation ExampleTableviewSubProductDetail
  @synthesize delegate;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];

if([delegate respondsToSelector:@selector(titlechange:)])
{
    //send the delegate function with the amount entered by the user
    [delegate titlechange:20];
}

HostProductdetailViewController.h中的代码

 #import "ViewPagerController.h"
 #import "ExampleTableviewSubProductDetail.h"

 @interface HostProductdetailViewController :  ViewPagerController <ViewPagerDataSource, ViewPagerDelegate, EnterAmountDelegate>
{
}

HostProductdetailViewController.m中的代码

  - (void)viewDidLoad {
[super viewDidLoad];
self.dataSource = self;
self.delegate = self;

 }
 -(void)titlechange:(NSInteger)amount
  {
   NSLog(@"sdfsf");
  }

在viewwillapper下面的Line总是返回false

 if([delegate respondsToSelector:@selector(titlechange:)])

如果我遗漏了什么,请告诉我。

谢谢

【问题讨论】:

  • ViewPagerController的类层次结构是什么?

标签: ios iphone methods delegates delegation


【解决方案1】:

从 HostProductdetailViewController 推送到 ExampleTableviewSubProductDetail 时需要设置 exampleTableviewSubProductDetail.delegate = self

【讨论】:

  • 谢谢@krisJones 这行得通....如果您能给出一些解释,那就太好了.... :)
  • 您需要将 exampleTableviewSubProductDetail.delegate 设置为 self 以便设置类委托,否则将不会设置委托。将其视为设置变量。由于 HostProductdetailViewController 符合 EnterAmountDelegate,我们可以将该控制器设置为 exampleTableviewSubProductDetail 委托。希望这能解释它。 raywenderlich.com/46988/ios-design-patterns 在 How to Use the Delegate Pattern 中给出了很好的解释
【解决方案2】:

当我在您的代码中看到其他一些潜在的危险内容时,请尝试检查此示例。它由 2 个通过委托连接的简单类组成。注意对委托的强引用,因为您的这段代码会产生保留周期并导致内存泄漏。

协议:

// defining a custom protocol
@protocol PingProtocol <NSObject>
- (void)didPing;
@end

Ping 类:

//
// This class will be able to send notifications via delegate for the protocol PingProtocol
// Any object that implements PingProtocol will be able to assign itself to the delegate property and will be notified to all protocol methods
//
@interface PingClass : NSObject

// The listener object that implements PingProtocol
// Note this should be weak or there will a retain cycle
@property (nonatomic, weak) id<PingProtocol> delegate;

@end

@implementation PingClass

// Some event that happens will check if the delegate actually implements this method and call it.
// The respondsToSelector is not necessary in this case since the method is not optional though.
- (void)onEvent:(id)sender
{
    if([self.delegate respondsToSelector:@selector(didPing)])
    {
        [self.delegate didPing];
    }
}

// Will create a timer which will call onEvent: every second.
// Note there should be some way to invalidate the timer as this will cause a memory leak for the PingClass
- (void)startPing
{
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(onEvent:) userInfo:nil repeats:YES];
}

@end

听众:

//
// This class will listen to PingProtocol methods.
// It will need to implement all non-optional methods defined by PingProtocol
//
@interface ListenerClass : NSObject<PingProtocol>

@property (nonatomic, strong) PingClass *someClass;

@end

@implementation ListenerClass

// will create a PingClass object and asign itself as a delegate to start listening to delegate methods
- (void)startListening
{
    self.someClass = [[PingClass alloc] init];
    self.someClass.delegate = self;
    [self.someClass startPing];
}
// A protocol method
- (void)didPing
{
    NSLog(@"Ping");
}

@end

【讨论】:

  • 感谢您宝贵的时间和精力...我会在我的代码中考虑这些更改... :)
【解决方案3】:

你很可能错过了self

if([self.delegate respondsToSelector:@selector(titlechange:)])

你需要注意这些事情。在您的情况下,委托更接近函数指针,然后是实际对象。您也可以通过_delegate 访问它。

【讨论】:

  • 然后在此处设置断点并记录您的委托。在断点处写入控制台“po [delegate class]”。还要检查代理是否真的存在(不是零)。
猜你喜欢
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
相关资源
最近更新 更多