[英]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 您可以使用
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.