0

我的应用程序中有几个UIPopover包含UITableViews 的 s。popoverControllerDidDismissPopover:当他们被解雇时,所有人都会发送消息。当一个特定的弹出框被关闭时,我想获取所有用户的选择并将它们移动到UITextView.

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

    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

4 回答 4

2

popoverControllerDidDismissPopover:委托方法告诉您正在关闭哪个弹出框。从那里您可以根据需要查看弹出框contentViewController

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

    UITableView *tableView = controller.tableView;
}

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

于 2013-04-10T21:50:47.770 回答
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
于 2013-04-10T22:32:27.677 回答
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
    }
}
于 2013-04-10T22:59:01.590 回答
0

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

于 2013-04-10T22:38:55.817 回答