Satyam
Satyam

Reputation: 15894

iOS - Storyboard - Delegate for pop over

I'm using storyboard in my ipad application and successfully able to do transitions, use segues etc. Currently I am showing pop over view controller on click of a button. I want to detect when the pop over dismisses. How can I do it?

Upvotes: 8

Views: 10749

Answers (5)

Kumar C
Kumar C

Reputation: 553

An Objective-C code for the question is below.

if ([segue.identifier isEqualToString:@"home_login"]) 
{
   UIViewController *dest = segue.destinationViewController;
   dest.popoverPresentationController.delegate = self;
}

- (BOOL) popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController
{
    return NO;
}

Upvotes: 0

ergoon
ergoon

Reputation: 1264

Since UIStoryboardPopoverSegueis deprecated in iOS 9, you can use a UIStoryboardPopoverPresentationSegue.

Then in prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)you can set the delegate like this:

Swift:

if let identifier = segue.identifier where identifier == "showPopover" {
    let destVC = segue.destinationViewController as! UIViewController

    destVC.popoverPresentationController?.delegate = self
}

Upvotes: 1

Christopher
Christopher

Reputation: 6046

Here is what I did:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"popover"])
    {
        UIStoryboardPopoverSegue *pop = (UIStoryboardPopoverSegue*)segue;
        pop.popoverController.delegate = self;
    }
}

Upvotes: 14

Satyam
Satyam

Reputation: 15894

Create a segue in view controller:

@property (strong, nonatomic) UIStoryboardPopoverSegue* popSegue;

In XIB, create an identifier called "popover" for the view.

In Interface, write the following code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if( [[segue identifier] isEqualToString:@"popover"] )
    {
        //[[segue destinationViewController] setDelegate:self];
        NSLog(@"%@",[[segue destinationViewController] viewControllers]);
        self.popSegue = (UIStoryboardPopoverSegue*)segue;
.
.
.
}

Write the following code to dismiss the pop over by coding:

[self.popSegue.popoverController dismissPopoverAnimated:YES];        

Upvotes: 4

Ryan Wersal
Ryan Wersal

Reputation: 3228

UIPopoverController

Now with my revelation that you're talking about a UIPopoverController, here are the steps:

  1. Setup the UIPopoverController with an appropriate delegate (I'm assuming the "sender" view controller)

  2. Have your "sender" conform to the UIPopoverControllerDelegate

  3. Implement the – popoverControllerDidDismissPopover: message and have any detection logic here

  4. Implement - prepareForSegue:sender: and use the segue's destinationController to both get a reference and set the delegate, something like below:

    ((MyViewController*)segue.destinationController).delegate = self;


Modal View Controller

  1. Add a delegate to the view controller that is being presented
  2. Name your segue if you haven't already
  3. Have your base view controller implement - prepareForSegue:sender: (refer to the UIViewController documentation)
  4. Assign the sending view controller as the modal view controller's delegate in prepareForSegue:sender:
  5. Call a desired method on the delegate immediately before or after you call dismissModalViewControllerAnimated:

That is how I would approach this. I would also recommend having a formal protocol to conform your sending view controller with.

Upvotes: 7

Related Questions