代表不工作

时间:2011-05-15 14:16:14

标签: objective-c ios protocols uipopovercontroller

我正在尝试解除弹出窗口并同时传输数据。我实现了一个代理DismissPopoverDelegate,但它无法正常工作。但没有错误。如果点击了保存按钮,它会对其进行注册,并在调用委托后完成该行。但它不起作用......

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];
    }
}

2 个答案:

答案 0 :(得分:1)

包含.h文件时,您有一个循环引用。

dateViewPopOverViewController_iPad.h包含AddEventViewController_iPad.hAddEventViewController_iPad.h包含dateViewPopOverViewController_iPad.h,这会导致编译器引发错误。

使用协议的目的之一是避免这种循环引用。从dateViewPopOverViewController_iPad.h文件中删除AddEventViewController_iPad.h包含可能会解决问题

答案 1 :(得分:0)

您对dismissPopoverAnimated的通话不会触发对代表的通话。来自Apple的UIPopoverDelegate文档:

弹出控制器不会调用此方法来响应对dismissPopoverAnimated方法的编程调用。如果以编程方式关闭popover,则应在调用dismissPopoverAnimated方法后立即执行任何清理操作。