2015-10-06 53 views
0

以下是我的代碼,沒有錯誤,但選擇器沒有響應。自定義deleget沒有響應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下面一行總是返回false

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

請讓我知道,如果我缺少什麼。

感謝

+0

什麼是'ViewPagerController'的類層次? –

回答

2

當從HostProductdetailViewController推到ExampleTableviewSubProductDetail您需要設置exampleTableviewSubProductDetail.delegate =自

+0

謝謝@krisJones這工作....這將是偉大的,如果你可以給一些解釋.... :) – Satish

+0

你需要設置exampleTableviewSubProductDetail.delegate爲self,所以類委託設置,否則委託將不會組。把它看作設置一個變量。由於HostProductdetailViewController符合EnterAmountDelegate,我們可以將該控制器設置爲exampleTableviewSubProductDetail委託。希望這可以解釋它。 http://www.raywenderlich.com/46988/ios-design-patterns在如何使用委託模式給出了一個很好的解釋 – KrisJones

0

最有可能你缺少self

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

你需要注意這些的東西。您的案例中的委託更接近函數指針,然後是實際對象。您也可以通過_delegate訪問它。

+0

感謝您的回覆,我也嘗試過,但仍然沒有運氣.. – Satish

+0

然後設置斷點在那裏並記錄你的代表。在斷點處寫入CONSOL「po [委託類]」。還要檢查代表是否實際存在(而不是零)。 –

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 
+0

感謝您寶貴的時間和精力......我會考慮我的代碼中的這種變化...... :) – Satish