在ViewController之间通过属性传递数据



我在viewController a上有一个字典它?

ViewController A:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if (cell== nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    if(_List.count>0) {
        [cell setupCell:[_List objectAtIndex:indexPath.row]];
        ViewControllerB *vc=[[ViewController alloc]init];
        vc.userIdInfo=[_List objectAtIndex:indexPath.row];
    }
    return cell;
}

viewController b:

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"_userIdInfo %@",userIdInfo);
    if([userIdInfo valueForKey:@"UserID"]) {
        [self fetchDataFromServer:getdetail];
    }
}

NSLog(@"_userIdInfo %@",userIdInfo)打印nullvc.userIdInfo打印字典

{
    Count = 3;
    UserID = "6gdab241-j176-1121-lde2-as8d21f62231";
    UserName = "abc@gmail.com";
}

在目标C中, cellforrowatIndExpath 方法用于显示数据。根据代码,您是在 cellforrowatIndExpath 中的ViewController B进行Intiallell。它将多次分配ViewController b,从而导致内存消耗。

我建议您在ViewController a。

的ViewDidload中初始化

要将数据从A到B传递,您需要使用 didSelectRowatIndExpath uitaiteViewDelegate的方法。

示例:

ViewController A:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (cell== nil) {
    cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
if(_List.count>0) {
   //Use to show data
   cell.textLabel.text = [NSString stringWithFormate:@"%@",[_List objectAtIndex:indexPath.row]]];
   // [cell setupCell:[_List objectAtIndex:indexPath.row]];
   // ViewControllerB *vc=[[ViewController alloc]init];
   // vc.userIdInfo=[_List objectAtIndex:indexPath.row];
}
return cell;

}

viwecontroller b

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   // 1st Type
   ViewControllerB *vc=[[ViewController alloc]init];
   vc.userIdInfo=[_List objectAtIndex:indexPath.row];
   [self.navigartionController pushViewController:aVC animated:YES];
}

您需要在以下选择中选择" view Controller B初始化"。

不要在cellForRowAtIndexPath中初始化它。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 1st Type
    ViewControllerB *vc=[[ViewController alloc]init];
    vc.userIdInfo=[_List objectAtIndex:indexPath.row];
    [self.navigartionController pushViewController:aVC animated:YES];
    //2nd Type
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    ViewControllerB *aVC = [storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
    aVC.userInfo = <set your data>;
    [self.navigartionController pushViewController:aVC animated:YES];
}

希望这会有所帮助。

代码

 ViewControllerB *vc=[[ViewController alloc]init];
 vc.userIdInfo=[_List objectAtIndex:indexPath.row];

仅创建ViewController;它不会在任何地方展示。此外,在离开

的范围后,实例 vc 将立即进行交易。
if(_List.count>0){}

您的方法

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"_userIdInfo %@",userIdInfo);
    if([userIdInfo valueForKey:@"UserID"])
    {
        [self fetchDataFromServer:getdetail];
    }
}

永远不会被调用,因为您的视图永远不会加载。

我认为,您真正想要的是使用navigationcontroller实例或当前ViewController

向您展示VC实例
[self.navigationController presentViewController: vc animated: YES completion: nil];

最新更新