自定义删除不响应 ToSelector
custom deleget not responding ToSelector
以下是我的代码,没有错误,但选择器没有响应。
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 下面的行总是 return false
if([delegate respondsToSelector:@selector(titlechange:)])
如果我遗漏了什么,请告诉我。
谢谢
从 HostProductdetailViewController 推送到 ExampleTableviewSubProductDetail 时,您需要设置 exampleTableviewSubProductDetail.delegate = self
很可能你错过了self
:
if([self.delegate respondsToSelector:@selector(titlechange:)])
你需要注意这些事情。在您的例子中,委托更接近函数指针,然后是实际对象。您也可以通过 _delegate
访问它。
正如我在您的代码中看到一些其他潜在危险的东西一样,请尝试检查此示例。它由 2 个简单的 classes 组成,它们通过委托连接。注意对委托的强引用,因为你的这段代码会产生一个保留周期并导致内存泄漏。
协议:
// defining a custom protocol
@protocol PingProtocol <NSObject>
- (void)didPing;
@end
平class:
//
// 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
以下是我的代码,没有错误,但选择器没有响应。
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 下面的行总是 return false
if([delegate respondsToSelector:@selector(titlechange:)])
如果我遗漏了什么,请告诉我。
谢谢
从 HostProductdetailViewController 推送到 ExampleTableviewSubProductDetail 时,您需要设置 exampleTableviewSubProductDetail.delegate = self
很可能你错过了self
:
if([self.delegate respondsToSelector:@selector(titlechange:)])
你需要注意这些事情。在您的例子中,委托更接近函数指针,然后是实际对象。您也可以通过 _delegate
访问它。
正如我在您的代码中看到一些其他潜在危险的东西一样,请尝试检查此示例。它由 2 个简单的 classes 组成,它们通过委托连接。注意对委托的强引用,因为你的这段代码会产生一个保留周期并导致内存泄漏。
协议:
// defining a custom protocol
@protocol PingProtocol <NSObject>
- (void)didPing;
@end
平class:
//
// 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