为了解释我的情况,我有tableView
个自定义tableViewCell
,我在这个自定义单元格中创建了一个按钮。我希望此按钮添加已定义到box
的对象NSMutableArray
,并将此数组传递给restoCardConfirmationViewController
以创建购物车。
问题在于,当我尝试在NSMutablearray
中显示restoCardConfirmationViewController
时,结果为零。这是我的代码:
#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");
}
}
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface RestoCardConfirmationViewController : UIViewController
@property(nonatomic) NSMutableArray *boxesCommande;
#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.
}
答案 0 :(得分:1)
一种方法是代表团。创建一个委托,通知每个添加到RestoCardConfirmationViewController的对象。
另一个更简单的方法是:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.purchases count];
}
在MainViewViewController中,添加按钮单击事件,而不是在tableview单元格中使用它。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.button.tag = indexPath.row;
[cell.button addTarget:self
action:@selector(select)
forControlEvents:UIControlEventTouchUpInside];
// ...
}
这将调用以下方法,也在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");
}
}
现在在prepareforSeque中,将创建的数组传递给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;
}
}
答案 1 :(得分:0)
在addObject:
之后推送到ViewController
- (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];
}
答案 2 :(得分:0)
不要忘记在自定义视图控制器中初始化boxesCommande数组。
将以下行添加到@implementation中的RestoCardConfirmationViewController.m中:
- (instancetype)init {
if (self = [super init]) {
self.boxesCommande = [[NSMutableArray alloc] init];
}
return self;
}