0

I wrote some code to hide a popover pressing a button but...it doesn't work (nothing new). I have a main view (I will call it A) with a button inside, pressing the button a popover is shown (I will call it B). The popover has another button inside which I want to use to close the popover and do something in A. Im other words, A is my main view, pressing a button I show a popover (B) to execute a login (credentials can be filled in).

Pressing the login button (the one within the popover) I call a service to check credentials and, in case the user is authenticated I want then close the popover and update my main view (A).

Below is my code

Popover header file (B)

#import <UIKit/UIKit.h>
@protocol MyPopoverDelegate <NSObject>
    - (void) didLoginButton;
@end

@interface login : UIViewController
@property (nonatomic, assign) id<MyPopoverDelegate> delegate;

- (IBAction) doLogin;

@end

Popover implementation file (B)

#import "login.h"
@implementation login
@synthesize delegate;

....
..

- (IBAction) doLogin {
   [self.delegate didLoginButton];
}

The button is linked with the doLogin action and it works fine (within the popover). I have some logic inside and it works well.

Main view header file (A)

#import <UIKit/UIKit.h>
#import "login.h"

@interface ViewController : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate> {
   UIPopoverController *myPopoverController;
}

@property (nonatomic, retain) UIPopoverController *myPopoverController;
....
..

@end

Main view implementation file (A)

@synthesize delegate;
..
...

- (void) didLoginButton {
    if ([myPopoverController isPopoverVisible]) {
         NSLog(@"dismiss ok");

         [myPopoverController dismissPopoverAnimated:YES];
     }
}

....
..

Running my code and pressing the login button form the main view the popover appears. Pressing the button inside the popover nothing happens.

Basically, the didLoginButton method I have set into my main view file is never triggered and, of course, the popover is always visible. What I'm doing wrong?

Mario Catena
  • 575
  • 1
  • 8
  • 20

1 Answers1

1

Set the delegate

 BViewController.delegate = mainAViewController; //or self

before presenting the popover. If this is not your problem, please provide some code from your B creation and presentation.

Dave
  • 1,081
  • 6
  • 10
  • I'm showing the popover using a segue defined within my storybpard. Not sure how I can set my delegate there, so...I think you are right. I'm not setting the delegate in my B view (the popover view actually). Within my main view I have added the method prepareForSegue as below - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"login"]) { ((UIStoryboardPopoverSegue *)segue).popoverController.delegate = self; } } I check if the identifier is like "login" that is the same identifier I have set in my storyboard. – Mario Catena Nov 23 '12 at 15:47
  • I don't know how it should work with with storyboard. I don't use storyboard. – Dave Nov 23 '12 at 16:14