将数据传递到另一个View Controller不起作用

时间:2014-01-14 15:30:57

标签: ios objective-c uiviewcontroller

我正在尝试将数组传递给另一个视图控制器。但是,在接收方法已经执行之后,数据到达那里很好。所以在接收方法中,我正在处理一个空数组。

我在这里发送。

   - (void)mapView:(GMSMapView *)mapView didTapInfoWindowOfMarker:(GMSMarker *)marker
  {
      UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                         bundle: nil];
     UserProfileViewController *userProfileViewController = (UserProfileViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"UserProfileViewController"];


     [userProfileViewController fetchMarkerData:marker.snippet nameOfUser:marker.title];

userProfileViewController.membersDetailArray   = self.membersArray; // This is what I am sending

     UINavigationController *nv = [[UINavigationController alloc]initWithRootViewController:userProfileViewController];
    [self.revealSideViewController popViewControllerWithNewCenterController:nv animated:YES];


 }

这里我收到了数组

  //This executes first and shows null ... the viewDidLoad NSLog output shows after this.
   -(void)fetchMarkerData:(NSString *)typeOfMarker nameOfUser:(NSString *)username
  {
   NSLog(@"members detail array in fetch marker data - %@ ", self.membersDetailArray); // shows null
   }
    - (void)viewDidLoad
   {
     [super viewDidLoad];
     NSLog(@"members detail array  in View Did Load- %@ ", self.membersDetailArray);//shows data  
   }

3 个答案:

答案 0 :(得分:2)

您将数据传入以下方法:

-(void)fetchMarkerData:(NSString *)typeOfMarker nameOfUser:(NSString *)username {
    NSLog(@"members detail array in fetch marker data - %@ ", self.membersDetailArray); // shows null
}

但您没有更新任何属性,例如self.membersDetailArray(假设它填充了两个typeOfMarkerusername个对象。

另外,不要忘记初始化数组:

-(void)fetchMarkerData:(NSString *)typeOfMarker nameOfUser:(NSString *)username {
   self.membersDetailArray = @[typeOfMarker, username];
    NSLog(@"members detail array in fetch marker data - %@ ", self.membersDetailArray);
}

我不确定您希望如何存储数据(这两个对象似乎不适合使用数组,但至少可以解决当前null的问题。

答案 1 :(得分:2)

在您发布的代码中,您正在调用

[userProfileViewController fetchMarkerData:marker.snippet nameOfUser:marker.title];

在您实际发送此行中的数组之前:

userProfileViewController.membersDetailArray   = self.membersArray;

只需切换这两行,fetchMarkerData:nameOfUser:应该具有self. membersDetailArray的正确值。

此外,您应该知道viewDidLoad在导航控制器要显示该视图之前不会运行,在您调用的情况下是

[self.revealSideViewController popViewControllerWithNewCenterController:nv animated:YES];

答案 2 :(得分:0)

尝试替换这些行:

     [userProfileViewController fetchMarkerData:marker.snippet nameOfUser:marker.title];

     userProfileViewController.membersDetailArray   = self.membersArray; // This is what I am sending

为此:

     //using the setter to pass your data before you call the fetchMarkerData
     [serProfileViewController setMembersDetailArray:self.membersArray]; // This is what I am sending 

     [userProfileViewController fetchMarkerData:marker.snippet nameOfUser:marker.title];