0

我可以对自定义代表使用一些帮助。我正在尝试制定一个协议,向其代表发送消息以关闭弹出视图。这是我正在尝试的。

在 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");
}

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

4

2 回答 2

1

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

popover.delegate = self

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

于 2012-05-31T20:27:02.617 回答
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.
}
于 2012-05-31T20:32:37.653 回答