确定哪个UIPopover控制器已被解除

时间:2013-04-10 21:46:23

标签: objective-c cocoa-touch uipopovercontroller

我的应用中有几个UIPopover个包含UITableView个。所有人在被解雇时都会发送消息popoverControllerDidDismissPopover:。当特定的弹出窗口被解除时,我想要取出所有用户的选择并将它们移动到UITextView

除非我知道哪个popover被解雇,否则我不能这样做。我有什么想法可以做到这一点吗?

    UIViewController* popoverContent = [[UIViewController alloc] init];

    UIView *popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 416)];  //  was 216
    popoverView.backgroundColor = [UIColor redColor];
    popoverContent.contentSizeForViewInPopover = CGSizeMake(300.0, 416.0);

    //  define UITableView
    tvServices = [[UITableView alloc] init];
    tvServices.frame = CGRectMake(0, 0, 300, 416);
    tvServices.tag = 1201;

    tvServices.delegate = self;
    tvServices.dataSource = self;

    //  add it to the popover
    [popoverView addSubview:tvServices];
    popoverContent.view = popoverView;
    popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
    popoverController.delegate = (id)self;
    [popoverController setPopoverContentSize:CGSizeMake(300, 416) animated:NO];

    //  show it next to services textbox
    [popoverController presentPopoverFromRect:soServices.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
}

4 个答案:

答案 0 :(得分:2)

popoverControllerDidDismissPopover:委托方法告诉您哪个弹出窗口正在被解除。从那里你可以根据需要查看popover的contentViewController

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    UITableViewController *controller = (UITableViewController *)popoverController.contentViewController;

    UITableView *tableView = controller.tableView;
}

这应该指向正确的目录。但是您不应该需要访问表视图。您正在从视图控制器而不是表视图中访问数据。

答案 1 :(得分:1)

你应该反过来做这件事:

您提供的控制器应使用委派来通知启动演示的控制器。

在弹出控制器上,您向代表(呈现控制器)发送一条消息,告知其选择/取消选择。


#import "ViewController.h"
#import "QuestionGroupViewController.h"

@interface ViewController () <QuestionGroupViewControllerDelegate>
@end

@implementation ViewController

- (IBAction)presentQuestionnaire:(id)sender {

    NSArray *q = @[@{@"question" : @"what cities would you like to visit?" , @"answers": @[@"Paris", @"barcelona", @"Istanbul"]}];

        QuestionGroupViewController *qvc = [QuestionGroupViewController new];
        qvc.delegate = self;
        qvc.questions = q;
        [self presentViewController:qvc animated:YES completion:NULL];
    }
}

-(void)selectedAnswer:(NSString *)answer forQuestion:(NSString *)question
{
    NSLog(@"selectedt\t\t%@: %@", question, answer);
}
-(void)deSelectedAnswer:(NSString *)answer forQuestion:(NSString *)question
{
    NSLog(@"deselectedt\t\t%@: %@", question, answer);
}
@end

#import <Foundation/Foundation.h>    
@protocol QuestionGroupViewControllerDelegate<NSObject>
-(void) selectedAnswer:(NSString *)answer forQuestion:(NSString *) question;
-(void) deSelectedAnswer:(NSString *)answer forQuestion:(NSString *) question;

@end

@interface QuestionGroupViewController : UITableViewController

@property (nonatomic, strong) NSArray *questions;
@property (nonatomic, weak) id<QuestionGroupViewControllerDelegate> delegate;
@end

#import "QuestionGroupViewController.h"


@interface QuestionGroupViewController ()
@property (nonatomic, strong) NSMutableArray *selectedCells;

@end


@implementation QuestionGroupViewController {

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.selectedCells = [NSMutableArray array];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.questions count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.questions[section][@"answers"] count] +1 ;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.questions[section][@"question"];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier =@"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:identifier];

    }
    if (indexPath.row == [self.questions[indexPath.section][@"answers"] count]) {
        cell.textLabel.text = @"done";
    } else {
        cell.textLabel.text = [[self.questions objectAtIndex:indexPath.section][@"answers"] objectAtIndex:indexPath.row];
    }


    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == [self.questions[indexPath.section][@"answers"] count]) {
        [self dismissViewControllerAnimated:YES completion:NULL];
        return;
    }

    NSString *answer = self.questions[indexPath.section][@"answers"][indexPath.row];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
        [self.selectedCells removeObject:indexPath];

        cell.accessoryType = UITableViewCellAccessoryNone;
        [self.delegate deSelectedAnswer: answer forQuestion:self.questions[indexPath.section][@"question"]];

    } else {
        [self.selectedCells addObject:indexPath];

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [self.delegate selectedAnswer: answer forQuestion:self.questions[indexPath.section][@"question"]];

    }

}

-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
    return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}

@end

答案 2 :(得分:1)

您应该在popoverControllerDidDismissPopover:

中执行此操作
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
    UITableView *tableView = (UITableView*)[popoverController.contentViewController.view viewWithTag:1201];
    if (tableView != nil) {
        // here you know that this UIPopover has tvServices as you set its tag to 1201
    }
}

答案 3 :(得分:0)

如果您要从代码中呈现弹出框,那么您应该参考它们。收到popover委托调用后,您所要做的就是检查哪个实例正在被解除。听起来很简单,除非我错过了什么。