aissa el ouafi
aissa el ouafi

Reputation: 1

Pass an object defined from a customed tableViewCell

To explain my situation, I have a tableView with custom tableViewCell and I have created a button in this customed cell. I want this button to add an object box which is already defined to an NSMutableArray and pass this array to restoCardConfirmationViewController in order to create a cart.

The problem is that when I try to display the NSMutablearray in restoCardConfirmationViewController, the result is nil. This is my code :

The boxTableViewCell :

#import "boxTableViewCell.h"
#import "RestoCardConfirmationViewController.h"
#import "RestauCardViewController.h"

@implementation boxTableViewCell {
NSMutableArray *_pickerPlace;
}
- (IBAction)select:(id)sender {
RestoCardConfirmationViewController *restauCardConfirmation = [[RestoCardConfirmationViewController alloc] init];
[restauCardConfirmation.boxesCommande addObject:_box];
NSLog(@"la box choisie est %@",_box);
self.select.enabled = NO;
if(!self.select.enabled){
    NSLog(@"button desabled");
}
}

The restoCardConfirmationViewController.h :

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface RestoCardConfirmationViewController : UIViewController
@property(nonatomic) NSMutableArray *boxesCommande;

The restoCardConfirmationViewController.m :

#import "RestoCardConfirmationViewController.h"
#import "ChoisirDateViewController.h"

@interface RestoCardConfirmationViewController ()

@end

@implementation RestoCardConfirmationViewController

- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"The NSMutableArray in the other %@",self.boxesCommande);
// Do any additional setup after loading the view.
}

Upvotes: 0

Views: 58

Answers (3)

Jen Jose
Jen Jose

Reputation: 4035

One approach is Delegation. Create a delegate that notifies every object added to RestoCardConfirmationViewController.

And the other simpler method is :

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [self.purchases count];
}

In the MainViewViewController, add button click event instead of using it in tableview cell.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     cell.button.tag = indexPath.row;
     [cell.button addTarget:self 
                         action:@selector(select)
               forControlEvents:UIControlEventTouchUpInside];
    // ... 
}

This calls the following method, also implemented in MainViewController

    - (IBAction)select:(id)sender {

     int index = ((UIButton *)sender).tag;//it returns the indexPath

    [self.boxesArray addObject: self.purchases[index]];
    NSLog(@"la box choisie est %@",_box);


    self.select.enabled = NO;
    if(!self.select.enabled){
        NSLog(@"button desabled");
    }
    }

Now in prepareforSeque, pass the created array to RestoCardConfirmationViewController.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        RestoCardConfirmationViewController *vc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        vc.boxesCommande = self.boxesArray;
    }
}

Upvotes: 1

Drayae
Drayae

Reputation: 241

Don't forget to initialize your boxesCommande array in your custom view controller.

Add the following lines to your RestoCardConfirmationViewController.m in the @implementation:

- (instancetype)init {
    if (self = [super init]) {
        self.boxesCommande = [[NSMutableArray alloc] init];
    }
    return self;
}

Upvotes: 0

Jay Bhalani
Jay Bhalani

Reputation: 4171

Push To ViewController After addObject :

- (IBAction)select:(id)sender 
{
    RestoCardConfirmationViewController *restauCardConfirmation = [[RestoCardConfirmationViewController alloc] init];
    [restauCardConfirmation.boxesCommande addObject:_box];
    NSLog(@"la box choisie est %@",_box);
    self.select.enabled = NO;
    if(!self.select.enabled)
    {
        NSLog(@"button desabled");
    }
    [self.navigationController pushViewController:restauCardConfirmation animated:YES];
}

Upvotes: 0

Related Questions