自定义委托,从另一个视图中关闭弹出窗口

时间:2012-05-31 20:05:21

标签: iphone xcode ipad uipopovercontroller popover

我可以使用自定义代理的一些帮助。我正在尝试制作一个协议,向其委托发送消息以解除弹出视图。这是我正在尝试的。

在popoverViewController.h

#import <UIKit/UIKit.h>


@protocol MyPopoverDelegate <NSObject>
-(void) didSelectLanguage;
@end



@interface Popover : UITableViewController{

    id <MyPopoverDelegate> delegate;
    NSMutableArray *languageData;

}

@property (nonatomic, assign) id <MyPopoverDelegate> delegate;

@end

的.m

@synthesize delegate;

...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"You selected %@", [languageData objectAtIndex:[indexPath row]]);
    [self.delegate didSelectLanguage];

}

...

在呈现弹出窗口的ViewController中

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




@interface ChoicesChoices : UIViewController <UIPopoverControllerDelegate, MyPopoverDelegate>{

    UIPopoverController *popover;




}
- (IBAction)facebook:(id)sender;
- (IBAction)twitter:(id)sender;
- (IBAction)sms:(id)sender;
- (IBAction)copy:(id)sender;
- (IBAction)email:(id)sender;
- (IBAction)home:(id)sender;
- (IBAction)mute:(id)sender;
- (IBAction)note:(id)sender;


@property (nonatomic, retain) UIPopoverController* popover;


@end

和.m

@synthesize popover;

...

- (void)didSelectLanguage{
[popover dismissPopoverAnimated:YES];
NSLog(@"didSelectLanguage fired");
}

当我在popover的表中选择一行时,didSelectLanguage不会被调用。关于我可能做错的任何想法?谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

确保在创建自定义类的实例时在呈现视图控制器中设置委托。

popover.delegate = self

此外,您的属性看起来像是标准的弹出控制器,而不是自定义视图控制器的实例。

答案 1 :(得分:1)

确保将您的委托设置为呈现弹出窗口的视图控制器。在ChoicesChoices.m

中有类似的内容
- (void)presentPopover
{
  // assuming ARC for all allocations
    Popover *myController = [Popover new];
    myController.delegate = self;
    self.popover = [[UIPopoverController alloc] initWithContentViewController:myController];
    [self.popover presentPopover...];  // two flavors here, FromRect: and FromBarButtonItem:, that's left up to you to choose which one is correct.
}