0

I'm Trying to dismiss a popover and transfer data at the same time. I implemented a delegate DismissPopoverDelegate but it is failing to work. But there are no errors. If the save button is tapped it registers it and it completes the line after where it calls the delegate. But its not working...

AddEventViewController_iPad.h

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <Foundation/Foundation.h>
#import "AboutSme.h"
#import "dateViewPopOverViewController_iPad.h"
#import "addPersonViewControllerPopover_iPad.h"
#import "PreviousEventsTableViewControllerPopover_iPad.h"

@interface AddEventViewController_iPad : UIViewController <UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate, UITextFieldDelegate, UIAlertViewDelegate,UIPopoverControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate,ABPeoplePickerNavigationControllerDelegate, ABNewPersonViewControllerDelegate, DismissPopoverDelegate> {
UIPopoverController *pop;

AddEventViewController_iPad.m

 - (IBAction) selectStartDate:(id) sender {
  NSLog(@"Select start date");
       dateViewPopOverViewController_iPad *dateViewPopOverViewController =     [[dateViewPopOverViewController_iPad alloc] init];
popover2 = [[UIPopoverController alloc]   initWithContentViewController:dateViewPopOverViewController];

 popover2.delegate = self;
  popover2.popoverContentSize = CGSizeMake(320, 460);

CGRect rect = CGRectMake(790, 170, 175, 300);

[popover2 presentPopoverFromRect:rect inView:self.view      permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

[dateViewPopOverViewController release];

/*
if (dateViewController == nil) {
    dateViewController = [[DateViewController_iPad alloc] initWithNibName:@"DateViewController_iPad" bundle:nil];
}

[self presentModalViewController:dateViewController animated:YES];

[dateViewController retain];
 */
}

- (void) dismissWithData:(NSString *)data
{
NSLog(@"%@", data);

[pop dismissPopoverAnimated:YES];
[pop release];
}

dateViewPopOverViewController_iPad.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@protocol DismissPopoverDelegate <NSObject>
@required
- (void) dismissWithData:(NSString *)data;

@end
@interface dateViewPopOverViewController_iPad : UIViewController {
IBOutlet UIDatePicker *datePicker;
IBOutlet UISegmentedControl *segmentedBar;
IBOutlet UILabel *startLabel;
IBOutlet UILabel *endLabel;
IBOutlet UISwitch *allDaySwitch;
NSDate *startDate;
NSDate *endDate;    
NSDate *now;
NSDateFormatter *dateFormatter;
id<DismissPopoverDelegate> delegate;
}

@property (retain) id delegate;
- (void) dismissWithData:(NSString *)data;



dateViewPopOverViewController_iPad.m

    @implementation dateViewPopOverViewController_iPad

    @synthesize startDate, endDate, datePicker, segmentedBar, startLabel, endLabel, now, allDaySwitch, delegate;

  - (IBAction) save:(id)sender {
    if ([self startDateIsValid] && [self endDateIsValid]) {
        //[[self parentViewController] setDatesForEvent:startDate eventEndDate:endDate allDay:[allDaySwitch isOn]];
  //  [self dismissModalViewControllerAnimated:YES];
        NSLog(@"works");
       [self.delegate dismissWithData:@"Some text from popover"];
  NSLog(@"works1");
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Ooops!" message:@"Please check the dates! Remember the end date must occur after the start date for the event to save." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
BDGapps
  • 3,318
  • 10
  • 56
  • 75
  • And just to point it out, you should not retain the delegate. you should have its property as assign to avoid circular references (which will cause a memory leak). check the answers at this post http://stackoverflow.com/questions/5811590/release-a-viewcontroller-correctly-that-sets-it-self-as-delegate-to-other-classes/5813247#5813247 – Felipe Sabino May 15 '11 at 14:27
  • Ok that took some errors away but still there is an error Cannot find protocol declaration.... – BDGapps May 15 '11 at 14:29

2 Answers2

1

You have a circular reference when including the .h files.

dateViewPopOverViewController_iPad.h includes AddEventViewController_iPad.h and AddEventViewController_iPad.h includes dateViewPopOverViewController_iPad.h, which will cause the compiler to raise an error.

One of the aims to use protocols is to avoid this kind of circular reference. Removing the dateViewPopOverViewController_iPad.h include from your AddEventViewController_iPad.h file might fix the problem

Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112
  • Now it is the time to put some logs in the methods and see which one on the flow is not being called. Try not just to say 'it is not working' as it does not provide enough information for people to help you – Felipe Sabino May 15 '11 at 15:43
  • so? what are the log results? Do you ever set the delegate property for the dateViewPopOverViewController_iPad object after you create it? – Felipe Sabino May 15 '11 at 15:56
  • It jumps over that line because it never retrieves any data in the addeventview thats what the long says. – BDGapps May 15 '11 at 16:43
  • 1
    What line? Sorry, but you have to be less laconic or rephrase your sentences better... You are not expressing yourself in a clear way – Felipe Sabino May 15 '11 at 17:17
  • NSLog(@"works"); [self.delegate dismissWithData:@"Some text from popover"]; NSLog(@"works1"); – BDGapps May 15 '11 at 17:17
  • So, I will ask again: Do you ever set the delegate property for the dateViewPopOverViewController_iPad object after you create it? – Felipe Sabino May 15 '11 at 17:19
  • I don't think so what do you mean by set it? – BDGapps May 15 '11 at 17:22
  • 1
    last week you asked almost the same question and accepted the answer on http://stackoverflow.com/questions/5922254/how-to-dismiss-uipopover-from-a-button-in-the-popover Take a look at the code and check the like where it does `vc.delegate = self;`. – Felipe Sabino May 15 '11 at 17:25
  • I did follow it but I'm getting the same problem. I just added the code above where it calls the popover. Is there anything wrong there??? – BDGapps May 15 '11 at 17:35
  • Yes, there is. You are setting the wrong object delegate to your class. Try replacing `popover2.delegate = self` by `dateViewPopOverViewController.delegate = self` – Felipe Sabino May 15 '11 at 17:38
  • 1
    just an add on. Try always to use the delegate property as assign with the right type like `@property (assign) id delegate;`, also check if you delegate is not null and if it responds to the selector you want to call `if ([self.delegate && [self.delegate respondsToSelector:@selector(dismissWithData:)]) { [self.delegate dismissWithData:@"Some text from popover"]; }` – Felipe Sabino May 15 '11 at 17:51
  • I have a totally different question now. I have a different way of calling the view for the iphone so it save the data while in these two view. How can i make it work on the ipad ? - (IBAction) selectStartDate:(id)sender { NSLog(@"Select start date"); if (dateViewController == nil) { dateViewController = [[DateViewController alloc] initWithNibName:@"DateViewController" bundle:nil]; } [self presentModalViewController:dateViewController animated:YES]; [dateViewController retain]; } – BDGapps May 15 '11 at 23:36
0

Your call to dismissPopoverAnimated will not trigger the call to the delegate. From Apple's UIPopoverDelegate documentation:

The popover controller does not call this method in response to programmatic calls to the dismissPopoverAnimated method. If you dismiss the popover programmatically, you should perform any cleanup actions immediately after calling the dismissPopoverAnimated method.

Dan
  • 10,531
  • 2
  • 36
  • 55
meelawsh
  • 617
  • 6
  • 8