你应该反过来做:
您呈现的控制器应该使用委托来通知开始演示的控制器。
在弹出的控制器中,您向代理(呈现控制器)发送一条消息,以通知它有关选择/取消选择的信息。
#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