Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在函数之间传递后数据丢失_Ios_Objective C - Fatal编程技术网

Ios 在函数之间传递后数据丢失

Ios 在函数之间传递后数据丢失,ios,objective-c,Ios,Objective C,关于NSDictionary上丢失的数据(或我试图在视图控制器之间传递的任何数据类型),我在这里遇到了问题。我正在传递数据,我将在下面的代码中显示。但是在传递并再次访问数据之后,我收到了一个null对象 FirstViewController.m -(void)showSecondViewController{ SecondViewController *secondVC = [SecondViewController new]; NSMutableDictionary *passedD

关于NSDictionary上丢失的数据(或我试图在视图控制器之间传递的任何数据类型),我在这里遇到了问题。我正在传递数据,我将在下面的代码中显示。但是在传递并再次访问数据之后,我收到了一个
null
对象

FirstViewController.m

-(void)showSecondViewController{
  SecondViewController *secondVC = [SecondViewController new];
  NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
  [passedData setObject:@"First Object" forKey:@"first_obj"];
  [passedData setObject:@"Second Object" forKey:@"second_obj"];
  [secondVC setData:passedData];
  [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
}
-(void)setData(NSDictionary *)data{
   passedDataContainer = data;
}
-(void)usePassedData{
  NSLog(@"Display Passed Data from FirstViewController: %@",data);
}
在my
SecondViewController.h中

 @interface SecondViewController : UIViewController
{
   NSDictionary *passedDataContainer;
}

-(void)setData(NSDictionary *)data;
@property(strong, nonatomic) NSDictionary *data;
SecondViewController.m

-(void)showSecondViewController{
  SecondViewController *secondVC = [SecondViewController new];
  NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
  [passedData setObject:@"First Object" forKey:@"first_obj"];
  [passedData setObject:@"Second Object" forKey:@"second_obj"];
  [secondVC setData:passedData];
  [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
}
-(void)setData(NSDictionary *)data{
   passedDataContainer = data;
}
-(void)usePassedData{
  NSLog(@"Display Passed Data from FirstViewController: %@",data);
}
由此,我假设我从我的
FirstViewController
中得到了我的
NSDictionary
,但是当我使用它时,比如说

-(void)usePassedData{
  NSLog(@"Display Passed Data from FirstViewController: %@",passedDataContainer);
}
在这个代码段中,输出将是

Display Passed Data from FirstViewController: (null)
我希望我已经很好地解释了这个案例。我已经检查了我在这里找到的一些可能的修复,例如,这篇文章-->。如果我知道这是什么原因,我会很高兴,因为我对Objective-C不熟悉

另外,我正在使用ARC


谢谢。

如果要将数据从secondviewcontroller传递到first view controller,则可以使用自定义委托,使用自定义委托,可以将数据从secondviewcintroller传递到firstviewcontroller

链接可能对您有所帮助。
这是了解学员的链接。

有许多方法可以实现您的解决方案

  • 默认值
  • 代理(自定义)

  • 针对您的问题:
    使用对NSDictionary的@PRoperty(Strong)引用,并从FirstViewController使用SecondViewController实例的适当alloc引用它。

    尝试这样做,希望这对您有所帮助:)

    在您的
    SecondViewController.h


       @synthesize data; //just synthesize it
    
    -(void)showSecondViewController{
     SecondViewController *secondVC = [SecondViewController new];
     NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
     [passedData setObject:@"First Object" forKey:@"first_obj"];
     [passedData setObject:@"Second Object" forKey:@"second_obj"];
      secondVC.data = passedData; //[secondVC setData:passedData];//change this
     [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    
    SecondViewController.m

    -(void)showSecondViewController{
      SecondViewController *secondVC = [SecondViewController new];
      NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
      [passedData setObject:@"First Object" forKey:@"first_obj"];
      [passedData setObject:@"Second Object" forKey:@"second_obj"];
      [secondVC setData:passedData];
      [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    
    -(void)setData(NSDictionary *)data{
       passedDataContainer = data;
    }
    
    -(void)usePassedData{
      NSLog(@"Display Passed Data from FirstViewController: %@",data);
    }
    

       @synthesize data; //just synthesize it
    
    -(void)showSecondViewController{
     SecondViewController *secondVC = [SecondViewController new];
     NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
     [passedData setObject:@"First Object" forKey:@"first_obj"];
     [passedData setObject:@"Second Object" forKey:@"second_obj"];
      secondVC.data = passedData; //[secondVC setData:passedData];//change this
     [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    

       @synthesize data; //just synthesize it
    
    -(void)showSecondViewController{
     SecondViewController *secondVC = [SecondViewController new];
     NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
     [passedData setObject:@"First Object" forKey:@"first_obj"];
     [passedData setObject:@"Second Object" forKey:@"second_obj"];
      secondVC.data = passedData; //[secondVC setData:passedData];//change this
     [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    
    和您的
    FirstViewController.m

    -(void)showSecondViewController{
      SecondViewController *secondVC = [SecondViewController new];
      NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
      [passedData setObject:@"First Object" forKey:@"first_obj"];
      [passedData setObject:@"Second Object" forKey:@"second_obj"];
      [secondVC setData:passedData];
      [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    
    -(void)setData(NSDictionary *)data{
       passedDataContainer = data;
    }
    
    -(void)usePassedData{
      NSLog(@"Display Passed Data from FirstViewController: %@",data);
    }
    

       @synthesize data; //just synthesize it
    
    -(void)showSecondViewController{
     SecondViewController *secondVC = [SecondViewController new];
     NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
     [passedData setObject:@"First Object" forKey:@"first_obj"];
     [passedData setObject:@"Second Object" forKey:@"second_obj"];
      secondVC.data = passedData; //[secondVC setData:passedData];//change this
     [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    


    根据您的解释,您正在使用
    故事板序列
    第一视图控制器
    转换到
    第二视图控制器
    。您需要以
    prepareforsgue
    方法而不是
    showSecondViewController
    方法传递数据

       @synthesize data; //just synthesize it
    
    -(void)showSecondViewController{
     SecondViewController *secondVC = [SecondViewController new];
     NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
     [passedData setObject:@"First Object" forKey:@"first_obj"];
     [passedData setObject:@"Second Object" forKey:@"second_obj"];
      secondVC.data = passedData; //[secondVC setData:passedData];//change this
     [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    
    试试这个

    FirstViewController.m
    
    -(void)showSecondViewController{
    
      [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
         if ([segue.identifier isEqualToString:@"SecondViewControllerSegue"]){
         NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
         [passedData setObject:@"First Object" forKey:@"first_obj"];
         [passedData setObject:@"Second Object" forKey:@"second_obj"];
    
         SecondViewController *secondVC = (SecondViewController *)segue.destinationViewController;
        [secondVC setData:passedData];
    }
    
    SecondViewController.h

     @interface SecondViewController : UIViewController
    {
       NSDictionary *passedDataContainer;
    }
    
    -(void)setData(NSDictionary *)data;
    
    @property(strong, nonatomic) NSDictionary *data;
    
    SecondViewController.m中

    -(void)showSecondViewController{
      SecondViewController *secondVC = [SecondViewController new];
      NSMutableDictionary *passedData = [[NSMutableDictionary alloc]init];
      [passedData setObject:@"First Object" forKey:@"first_obj"];
      [passedData setObject:@"Second Object" forKey:@"second_obj"];
      [secondVC setData:passedData];
      [self performSegueWithIdentifier:@"SecondViewControllerSegue" sender:self];
    }
    
    -(void)setData(NSDictionary *)data{
       passedDataContainer = data;
    }
    
    -(void)usePassedData{
      NSLog(@"Display Passed Data from FirstViewController: %@",data);
    }
    

    为什么不使用属性?此教程链接可能会帮助您: