简体   繁体   English

如何从detailViewController填充数组并返回iOS中的masterViewController?

[英]How to populate an array from detailViewController and return to masterViewController in iOS?

I have a tableView from which I navigate to a 'viewController' ie PassengerInfoViewController in which I have a form.I fill up the form , make a dictionary out of it and add it to a NSMutableArray . 我有一个tableView ,我可以从中导航到一个'viewController',即其中有一个表格的PassengerInfoViewController 。我填写表格,从中制作dictionary并将其添加到NSMutableArray

So when I am going back to the tableView' I would like to pass this array and then reload the tableView` with the filled array. 因此,当我返回tableView' I would like to pass this array and then reload the使用填充的数组tableView' I would like to pass this array and then reload the tableView`。

So here's what I am doing : - 所以这是我在做什么:-

  //navigate to the form
  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

  PassengerInfoViewController *pivc = [self.storyboard instantiateViewControllerWithIdentifier:@"PassengerInfoViewController"];
  [self.navigationController pushViewController:pivc animated:YES];

}

 //After filling up the form 

 -(void)goBackToPassengerDetails:(UIButton*)sender{

   NSString *title = self.titleTextField.text;
   NSString *fname = self.firstNameTextField.text;
   NSString *lname =self.lastNameTextField.text;
   NSString *ageStr = self.ageTextField.text;

   NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
   [dict setValue:title forKey:@"title"];
   [dict setValue:fname forKey:@"firstName"];
   [dict setValue:lname forKey:@"lastName"];
   [dict setValue:ageStr forKey:@"age"];
   Passenger *passengerObj = [Passenger sharedInstance]; //Singleton

   [passengerObj.passengerDetailArray addObject:dict];

    PassengerDetailsViewController *pdc = [self.storyboard instantiateViewControllerWithIdentifier:@"PassengerDetailsViewController"];
[pdc getPassengerinfo:passengerObj.passengerDetailArray];
[self.navigationController popViewControllerAnimated:YES];

Once I navigate back I reload the table view.However celForRow method doesn't populate new values. 导航后,我会重新加载表格视图。 celForRow方法不会填充新值。

 -(void)getPassengerinfo:(NSMutableArray*)arr{
  passenger_infoArray =[[NSMutableArray alloc]init];
  passenger_infoArray = arr;
  NSLog(@"Passenger Info Array : %@", passenger_infoArray);//Shows array of dictionaries

  [passengerInfoTableView reloadData];
}

My cellForRow method looks like this: 我的cellForRow方法如下所示:

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   //Not called when I am doing reload data
   static NSString *CellIdentifier = @"Cell";
   cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil)
   {
       cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier ];

   }
   NSLog(@"Table view array  : %@",passenger_infoArray); 
   if (passenger_infoArray.count>0) {

    NSString *temp= [[passenger_infoArray objectAtIndex:indexPath.row]objectForKey:@"firstName"];
    cell.textLabel.text = temp;
   }else{

    cell.textLabel.text = [NSString stringWithFormat:@"Passenger %ld", (long)indexPath.row+1];
    cell.imageView.image = [UIImage imageNamed:@"user.png"];

   }
     return cell;

  }

You can pass data using 您可以使用

  1. Delegates 代表
  2. Notifications 通知
  3. Properties 属性
  4. Global variables 全局变量

Using Delegates is good practice, and last two are not preferred. 使用代理人是一种很好的做法,而后两个不是首选。

You can pass data to previous view controller by using delegate method 您可以使用委托方法将数据传递到以前的视图控制器

In PassengerInfoViewController.h add below code just above of your import statement PassengerInfoViewController.h中,在您的import语句上方添加以下代码

@protocol passengerInfoViewControllerdelegate <NSObject>
     -(void)sendData:(NSMutableArray *)arrData; 
@end

After this create one property, as shown below 在此之后创建一个属性,如下所示

@property(nonatomic,assign)id delegate;

Now, in PassengerInfoViewController.m synthesize the property and after that 现在,在PassengerInfoViewController.m中合成属性,然后

-(void)viewWillDisappear:(BOOL)animated
{
    [delegate sendData:yourArray];
}

Now in tableview 现在在tableview

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PassengerInfoViewController *pivc = [self.storyboard instantiateViewControllerWithIdentifier:@"PassengerInfoViewController"];

    pivc.delegate = self;  //Add this line to your code

   [self.navigationController pushViewController:pivc animated:YES];
}

Now, it's time to implement delegate method which we created in viewcontroller 现在,该实现我们在viewcontroller中创建的委托方法了

-(void)sendData:(NSMutableArray *)arrData
{
      passenger_infoArray = arrData;
     [passengerInfoTableView reloadData];
}

Old way to do this was using delegates. 执行此操作的旧方法是使用委托。 Better way to do this is via an unwind segue because there's much less code and you don't have to create a protocol. 更好的方法是通过放松搜索,因为代码少得多,而且您不必创建协议。

Once your form completes, your PDC should call [self performSegue:@"theNameOfTheUnwindSegueeYouCreateInTheStoryboard"] . 表单填写完成后,您的PDC应该调用[self performSegue:@"theNameOfTheUnwindSegueeYouCreateInTheStoryboard"] Then, in your PDC's prepareForSegue method, get the destinationViewController property from the storyboardSegue parameter - this is your VC containing the Table View. 然后,在您的PDC的prepareForSegue方法中,从storyboardSegue参数获取destinationViewController属性-这是您的VC,其中包含表视图。 Set whatever model property you're using to back the table view - in your case it looks to be the passenger_infoArray object, which you'll have to make mutable if isn't already and also expose publicly if it isn't already. 设置您要用于支持表视图的任何模型属性-在您的情况下,它看起来是passenger_infoArray对象,如果尚未更改,则必须使其可变,如果尚未公开,则必须公开。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从MasterViewController访问DetailViewController - Unable to access DetailViewController from MasterViewController 在SplitViewController中从DetailViewController隐藏MasterViewController - Hide MasterViewController from DetailViewController in a SplitViewController 我应该如何从更改的masterViewController跟踪detailViewController? - how should I keep track of a detailViewController from a changing masterViewController? 使用prepareForSegue显示从MasterViewController到DetailViewController的字符串 - Displaying a string from MasterViewController to DetailViewController using prepareForSegue 在UISplitViewController应用程序中从DetailViewController获取MasterViewController - getting the MasterViewController from the DetailViewController in a UISplitViewController app 将数据从detailViewController向后传递到masterViewController - Pass data backward from detailViewController to masterViewController MasterViewController更新时如何刷新DetailViewController - How to refresh DetailViewController when MasterViewController is updated ISplitViewController:DetailViewController可以从MasterVIewController加载数据而不需要回传吗? - ISplitViewController: Can DetailViewController load data from MasterVIewController without segueing back? 从MasterViewController在DetailViewController中设置detailItem时出现奇怪的错误 - Weird Error when setting detailItem in DetailViewController from MasterViewController 弹出MasterViewcontroller时推送DetailViewController - push DetailViewController when popping MasterViewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM