为了解释我的情况,我有一个带有自定义 tableViewCell
的 tableView
,并且我在这个自定义单元格中创建了一个按钮。我希望此按钮添加一个已定义到 NSMutableArray
的对象 box
并将此数组传递给 restoCardConfirmationViewController
以创建购物车。
问题是,当我尝试在 restoCardConfirmationViewController
中显示 NSMutablearray
时,结果为零。这是我的代码:
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");
}
}
restoCardConfirmationViewController.h :
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
@interface RestoCardConfirmationViewController : UIViewController
@property(nonatomic) NSMutableArray *boxesCommande;
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.
}
最佳答案
一种方法是委派。创建一个委托(delegate),通知每个添加到 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;
}
}
关于ios - 传递从自定义 tableView Cell 定义的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30168286/