数组未通过委托从表视图传递到视图控制器

Array not being passed from tableview to view controller through Delegate

我正在尝试将数组从表视图控制器复制到视图控制器。我已经多次检查代码,似乎没问题。

//Delegate class
    #import <UIKit/UIKit.h>

    @protocol Category <NSObject>
    @required
    -(void) delegateMethodForCategory : (NSMutableArray *) arrayOfCategory;


    @end

    @interface Categories : UIViewController <UITableViewDelegate,UITableViewDataSource>

    @property (nonatomic) id<Category> delegate;
    @property (nonatomic,strong) NSArray *sports;
    @property (strong, nonatomic) IBOutlet UITableView *tableview;
    @property (nonatomic,strong) NSMutableArray *selectedIndexes;

    @end

//Delegate methods
    #import "Categories.h"
    @interface Categories ()
    {
           NSMutableArray *array ;
    }
    @end

    @implementation Categories

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        _sports= [[NSArray alloc] initWithObjects: @"Baseball", @"Soccer", @"Hockey",
                  @"Other",nil];


    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        // Return the number of rows in the section.
        return _sports.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        array = [[NSMutableArray alloc]init];

        // Configure the cell...

        cell.textLabel.text=[self.sports objectAtIndex:indexPath.row];
        return cell;
    }


    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

        NSString *cellText = selectedCell.textLabel.text;
        if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryNone)
        {   [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
            [array addObject:cellText];

        }else if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryCheckmark){
            [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryNone;
            [array removeObject:cellText];

        }


    }
    - (IBAction)doneButton:(id)sender {
        [self.delegate delegateMethodForCategory:array];
        [self dismissViewControllerAnimated:YES completion:nil];

    }

    @end
    #import <UIKit/UIKit.h>
    #import "Categories.h"

    @interface ActivityCreator : UIViewController <UIPopoverPresentationControllerDelegate, Category>
    @property   (nonatomic) Categories *requestClass;

    @property   (nonatomic,strong) NSMutableArray *arrayOfSports;

    @end

//This class implements delegate
    import "ActivityCreator.h"

    @interface ActivityCreator ()

    @end

    @implementation ActivityCreator

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self settingUp];
        // Do any additional setup after loading the view.
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    -(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
        return UIModalPresentationNone;
    }


    -(void)settingUp{
        _requestClass = [[Categories alloc]init];
        self.requestClass.delegate = self;

    }

    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

        if([segue.identifier isEqualToString:@"hubabuba"]){
            Categories *pop = (Categories *)segue.destinationViewController;
            pop.modalPresentationStyle = UIModalPresentationPopover;
            pop.popoverPresentationController.delegate = self;

        }
    }

    -(void) delegateMethodForCategory : (NSMutableArray *) arrayOfCategory {
        _arrayOfSports = arrayOfCategory;
        NSLog(@"%@",_arrayOfSports);

    }

任何我做错的指导都会有很大帮助。一直卡在这个上面。

委托方法根本没有被调用。

谢谢

在 prepareForSegue 方法中设置 Categories class 的委托,而不是在 settingUp 方法中设置。

 pop.delegate = self;

在 prepareForSegue 方法中。