开发者

Set delegate to parent view controller

开发者 https://www.devze.com 2023-03-09 17:59 出处:网络
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,

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 <AddSud开发者_StackOverflow社区okusViewControllerDelegate>{
[...]
}
- (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


Try this,

sudokuDetailViewController.delegate = [self.navigationController.viewControllers objectAtIndex:0];
0

精彩评论

暂无评论...
验证码 换一张
取 消