I have created three view controllers, namely FirstViewController, SecondViewController & ThirdViewController. The FirstViewController pushes SecondViewController, and similarly SecondViewController pushes ThirdViewController.
The ThirdViewController is a UITableViewController.
The SecondViewController and ThirdViewController have custom protocols. Both of them are pass some value to FirstViewController using their respective delegates.
The problem is that SecondViewController is successfully passing on the value to FirstViewController, but the ThirdViewController is not being able to do it.
Is it because the SecondViewController is directly being navigated from FirstViewController, so it is able to pass it, and ThirdViewController is not connected with the FirstViewController so it is not being able to pass the data?
Any help would be appreciated.
EDIT:
The FirstViewController just pushes SecondViewController through a button action.
The SecondViewController.h is:
@protocol secondViewDelegate <NSObject>
-(void)didCompleteSending:(NSArray *)array;
@end
@interface secondView : UIViewController
@property (nonatomic,strong) NSArray *footballPlayers;
@property (assign) id<secondViewDelegate>delegate;
- (IBAction)btnAction:(id)sender;
- (IBAction)btnGoToThirdView:(id)sender;
The SecondViewController.m is:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
footballPlayers = [[NSArray alloc]initWithObjects:@"Rooney",@"Van Persie",@"Ronaldo",@"Kagawa", nil];
}
- (IBAction)btnAction:(id)sender {
[delegate didCompleteSending:footballPlayers];
}
- (IBAction)btnGoToThirdView:(id)sender {
thirdView *objthirdView = [[thirdView alloc]init];
objthirdView.delegate=self;
[self.navigationController pushViewController:objthirdView animated:YES];
}
Similarly I have for ThirdViewController.
In FirstViewController.h
-(void)didSendDetailsFromThirdView:(NSString *)colors{
NSLog(@”selected string is = %@”,colors);
}
-(void)didCompleteSending:(NSArray *)array{
for (NSString *str in array) {
NSLog(@"%@",str);
}
}
The -(void)didSendDetailsFromThirdView:(NSString *)colors does not get called from ‘ThirdViewController’.
SECOND EDIT
- (IBAction)btnGoToThirdView:(id)sender {
FirstViewController *objFirstViewController = [FirstViewController alloc]init];
thirdView *objthirdView = [[thirdView alloc]init];
objthirdView.delegate=objFirstViewController;
[self.navigationController pushViewController:objthirdView animated:YES];
}
You are setting the delegate of the
ThirdViewControllerin the theSecondViewController.m to selfthis means that the delegate of the
ThirdViewControlleris theSecondViewControllerand not theFirstViewController. Keep a reference to theFirstViewControllerin theSecondViewControllerand use it as the delegate like this.Assuming it is not possible to pop from
ThirdViewControllerto theFirstViewControlleryou can maybe misuse theSecondViewControllerfor passing the data back to theFirstViewControllervia delegate methods you already implemented. But I would not recommend it – it is not a good design.