I want to call a method FromFilter:
located in ProductListViewController from other class FilterViewController ,
when i try to call a method i am getting nil value of delegate,
Here is my code snippet:
ProductListViewController.h
@protocol FirstControllerDelegate<NSObject>
@required
-(void)FromFilter:(NSString *)StrApi;
@end
@interface ProductListViewController : UIViewController<FirstControllerDelegate>{
....
}
@property (nonatomic, assign) id <FirstControllerDelegate> delegate;
ProductListViewController.m
-(void)FromFilter:(NSString *)StrApi{
NSLog(@"------- this is not getting called ---------");
}
FilterViewController.h
@interface FilterViewController : UIViewController{
...
}
@property (nonatomic, assign) id <FirstControllerDelegate> delegate;
FilterViewController.m
- (IBAction)ApplyClicked:(id)sender {
// ----- Here my _delegate is nil ------
if (_delegate == nil) {
[_delegate FromFilter:@"fuyds"];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Please help, where i am doing mistake?
It seems to be you didn't assign the delegate property of "FilterViewController".
whenever you create FilterViewController
Example :
FilterViewController *filterViewController = [[FilterViewController alloc]init];
filterViewController.delegate = self;//important
If you are presenting FilterViewController from ProductListViewController then you should assign the delegate as self
if (_delegate == nil) {
[_delegate FromFilter:@"fuyds"];
}
Of course your FromFilter
method isn't called. Your code calls this method of _delegate
object only if _delegate
is nil. Essentially, your code is this:
[nil FromFilter];
In Objective C you can send messages to nil
, they simply do nothing and return 0 (nil, NULL).
Did you try to change the code to:
- (IBAction)ApplyClicked:(id)sender {
// ----- Here my _delegate is nil ------
if (_delegate != nil) { // != NOT == --> you want to enter the if clause if the delegate is NOT nil
[_delegate FromFilter:@"fuyds"];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.