自定义删除无响应ToSelector

时间:2015-10-06 10:52:07

标签: ios iphone methods delegates delegation

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

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:)])

如果我错过了什么,请告诉我。

由于

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我在代码中看到其他一些有潜在危险的东西,请尝试查看此示例。它由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

答案 2 :(得分:0)

您很可能缺少self

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

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