我试图将数组从tableview控制器复制到视图控制器。我已经多次检查了代码,这似乎还可以。

//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类的委托,而不是在settingUp方法中进行设置。



 pop.delegate = self;


在prepareForSegue方法中。

10-08 12:30