1

I am creating the application for iPad with the UIPopover. I have a main view from which I am calling two popovers displaying different information. I was following the guidelines in How to Dismiss a Storyboard Popover thread and another threads, everything works fine except one thing. In my popover I have a button which triggers an action on the parent view. From time to time the action is triggered more than once even if the button was clicked just once and also popover was opened just once. My first assumption was that the popover was caching some data from several calls, but the problem seems to appear just randomly.

My configuration is: Mac OSx Snow Leopard with Xcode 4.2, iOS 5.0. Tested in Simulator, iPad 5.1 and iPad 6.0 all the same results.

I have the main view View 1 and the popover view View 2

In my view 2 I have a method ProceedButtonClicked which sends the notification to the View 1

- (IBAction) ProceedButtonClicked{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"proceedButtonClicked" object:self];
}

The method is binded to the button in the popover view. In view1 (parent view):

- (void)viewDidLoad
{
    [super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self   selector:@selector(ButtonClicked:) name:@"proceedButtonClicked" object:nil];
}

- (void) ButtonClicked:(NSNotification *) notification {
NSLog(@"I'm here ...");
//dismiss popover
if (paramPopover)
    [paramPopover dismissPopoverAnimated:YES];
}

I am pretty new to the iPad development, so maybe I am missing in my code something obvious, but searching until now resulted to nothing. Any help would be appreciated. Thank you.

Community
  • 1
  • 1
Pali Yes
  • 11
  • 1

1 Answers1

0

When using notifications, you risk multiple instances of a class answering the same notification, so if you have 2 controllers alive for some reason (bad memory management?), then when pressing the button 2 controllers will answer the call and you will have a duplicate action.

Buttons can have a specific callback assigned to them, and it's very simple to set it up by code:

If your button is a UIButton, you can set your target action like this:

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

If your button is a UIBarButtonItem, you set the target when you create it

[[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonAction:)];

Edit:

NSLog(@"I'm here ..."); That's creepy ...

Ismael
  • 3,927
  • 3
  • 15
  • 23
  • Thank you. I am assuming the bad memory management also, but I am using ARC and I don't know how to properly keep alive just one controller. Calling `popoverControllerDidDismissPopover` and set the `popOverController` to `nil` did not help either. I have tried also to change the UIButton action to the above code, the result is still the same. – Pali Yes Nov 26 '12 at 20:19