数组未通过Delegate从tableview传递到视图控制器

时间:2015-08-19 17:08:57

标签: ios xcode uitableview delegates uipopovercontroller

我正在尝试将数组从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);

    }

任何我做错的指导都会有很大的帮助。已经坚持了一段时间。

根本没有调用委托方法。

由于

1 个答案:

答案 0 :(得分:1)

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

Operator

在prepareForSegue方法中。