2

I am not entirely sure how to explain my problem but I will try.

So I have been trying to learn how to use delegates and protocols and ran into a problem. I have a protocol: ButtonInPopOverWasPressed, with one method:

   - (void)buttonWasPressed:(NSString *)buttonValue;

I also have a main view controller and a custom popover class with the property:

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

In my main view controller I have a button and a text label. When the button is pressed it segue's to a normal popover. I then assign the segue.destinationViewController's delegate to be the main view controller like so:

   [segue.destinationViewController setDelegate:self];    

Then when a button from the popover is selected it is displayed in the main view's text label by calling a protocol method that the main view listens for:

   [self.delegate buttonWasPressed:sender.currentTitle]; // sends the title of the button pressed to the delegate

This all works fine. My problem started when I wanted to have the popover transition to different views when a button was pressed instead of sending the information back to main view. But when I created a Navigation Controller as the popover and set the relationship to the old popover everything broke.

When I assign the delegate of the segue (shown above) it comes out on the other side as null so I completely lose my ability to pass it to other subviews and get information back to the main view. Does the Navigation Controller suck up the delegate? How do I get the delegate through the NC to the actual views?

I know this is Protocol and Delegate basics but I have looked around and can never seem to find an answer that has worked for me.

Firo
  • 15,448
  • 3
  • 54
  • 74

2 Answers2

3

Try the following code out.

    NSArray *temp = [[segue destinationViewController] childViewControllers];
    PopoverViewController *popoverViewController = (PopoverViewController)[temp objectAtIndex:0];
    popoverViewController.delegate = self;
lu yuan
  • 7,207
  • 9
  • 44
  • 78
  • Oh wow! Thank you so much! I have been working on this for quite some time. I was hoping it would be a simple fix! – Firo Jun 28 '12 at 18:19
0

Instead of using a navigation controller, create a delegate method in your main view that opens a different popover and closes the existing popover. You'll get the same result, but without the questionable idea of having a navigation controller in a popover.

Dustin
  • 6,783
  • 4
  • 36
  • 53