0

我正在使用 UITabBarController 构建简单的数据添加应用程序,来自 firstviewcontroller 的数据通过 customcell 存储并显示在 secondviecontroller 中,但问题是当我从标签栏中选择 secondviewcontroller 时未显示存储的数据。如何解决这个问题?

这是 FirstViewController 的编码,用于将 dataarray 传递给 secondviewcontroller。

 SecondViewController *svc = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
        [self presentModalViewController:svc animated:YES];

    finalArray = [[NSMutableArray alloc]init];

    [finalArray addObject:DataDic];

    svc.dataArray  = [[NSMutableArray alloc]initWithArray:finalArray];
}

和Secondcontroll.m 文件编码。

 #import "SecondViewController.h"
    #import "SaveDataCell.h"

 @implementation SecondViewController
@synthesize Dict1,dataArray,btninfo,btnBack;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Second", @"Second");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];
    }
    return self;
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    Dict1 = [[NSMutableDictionary alloc]init];



    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    dataArray = [[NSMutableArray alloc]init];

    NSLog(@"%@",dataArray);
}

- (void)viewDidAppear:(BOOL)animated
{


 [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}
- (NSInteger)numbe

rOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableVi

ew:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return[dataArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CatIdentifier";

    SaveDataCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = ( SaveDataCell*)[[[NSBundle mainBundle] loadNibNamed:@"SaveDataCell" owner:self options:nil] objectAtIndex:0];
    }


    cell.lblName.text = [[dataArray objectAtIndex:indexPath.row]valueForKey:@"Name"];
    cell.lblEmail.text = [[dataArray objectAtIndex:indexPath.row]valueForKey:@"Email"];
    cell.lblNo.text = [[dataArray objectAtIndex:indexPath.row]valueForKey:@"No"];
    cell.lblCity.text = [[dataArray objectAtIndex:indexPath.row]valueForKey:@"City"];
    cell.saveimg.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[[dataArray valueForKey:@"Image"]objectAtIndex:indexPath.row]]];
    return cell;
}
-(IBAction)ClicktoBack:(id)sender
{
    [self dismissModalViewControllerAnimated:YES];
}
-(IBAction)ClicktoInfo:(id)sender
{

}
4

3 回答 3

1

您可以通过多种方法在视图控制器之间传递数据。

喜欢

// 对于较低版本:

 SecondViewController *tmpNamesListVC = [self.tabBarController.viewControllers objectAtIndex:1];
     [tmpNamesListVC yourArray]= self.arraytoPass;

// 对于更高版本

SecondViewController *tmpNamesListVC = [self.tabBarController.viewControllers objectAtIndex:2];
 [tmpNamesListVC yourArray]= self.arraytoPass;

你也必须改变你的编码顺序

SecondViewController *svc = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

finalArray = [[NSMutableArray alloc]init];

[finalArray addObject:DataDic];

svc.dataArray  = [[NSMutableArray alloc]initWithArray:finalArray];

[self presentModalViewController:svc animated:YES];
于 2012-12-21T14:26:59.640 回答
0

尝试这个:

SecondViewController *svc = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

finalArray = [[NSMutableArray alloc]init];

[finalArray addObject:DataDic];

svc.dataArray  = [[NSMutableArray alloc]initWithArray:finalArray];

[self presentModalViewController:svc animated:YES];
于 2012-12-21T14:21:43.950 回答
0

在您的 SecondViewController 中创建一个属性 .. 如下所示:-

@property(strong,nonatomic)NSMutableArray * array;

并在“.m”文件中“@synthesize”。

现在,当您从“FirstViewController”导航时……请执行以下操作:-

SecondViewController *svc = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

finalArray = [[NSMutableArray alloc]init];

[finalArray addObject:DataDic];

svc.array  = finalArray

[self presentModalViewController:svc animated:YES];
于 2015-03-27T12:11:59.537 回答