Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set delegate to parent view controller

I have three view controllers in my app pushed to the navigation controller. When I'm on the third view controller I want to send a message to the first one. I think a delegate is the way to go here, but I'm not able to set it correctly.

In my third view controller's h-file I have this:

@protocol AddSudokusViewControllerDelegate

- (void)saveSudoku:(Sudoku *)sudoku;

@end

@interface [...]

    id<AddSudokusViewControllerDelegate> delegate;

[...]

@property (nonatomic, retain) id<AddSudokusViewControllerDelegate> delegate;

Then I synthesize it in the .m file.

In my first view controller I have this (.h):

@interface SudokusViewController : UITableViewController <AddSudokusViewControllerDelegate>{
[...]
}
- (void)saveSudoku:(Sudoku *)sudoku;

So far so good I think. Now I want to set the delegate of the third view controller to first when I create it in the second controller. I thought I could do it like this, but it doesn't work.

sudokuDetailViewController = [[SudokuDetailViewController alloc] init];
[sudokuDetailViewController setDelegate:[[self navigationController] parentViewController]];

[[self navigationController] pushViewController:sudokuDetailViewController animated:YES];

Am I doing this the right way or is there another approach when you have three controllers like this?

Best Regards Linus

like image 386
Linus Avatar asked Apr 19 '26 04:04

Linus


1 Answers

Try this,

sudokuDetailViewController.delegate = [self.navigationController.viewControllers objectAtIndex:0];
like image 192
Deepak Danduprolu Avatar answered Apr 21 '26 21:04

Deepak Danduprolu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!