Passing the data from ParentController to ChildController
Anyone knows how to do the code let's say:
First select each row (UITableView) on ParentViewController and then it will push to another controller called ChildViewController which allows the user to select the choices. Upon selecting the choices, how to get this "choices" to be passed back to ParentViewController -> put this data on cell.detailTextLabel.text.
ChildViewController.m (didSelectRowAtIndexPath)
//Obtain the data
NSString *selectString = [countArray objectAtIndex: [indexPath row]];
NSLog(@"The answer is %@", selectString);
* It is able to show the correct value but I am wondering how will I pass it back to ParentViewController.
First select each row (UITableView) on ParentViewController and then it will push to another controller called ChildViewController which allows the user to select the choices. Upon selecting the choices, how to get this "choices" to be passed back to ParentViewController -> put this data on cell.detailTextLabel.text.
ChildViewController.m (didSelectRowAtIndexPath)
//Obtain the data
NSString *selectString = [countArray objectAtIndex: [indexPath row]];
NSLog(@"The answer is %@", selectString);
* It is able to show the correct value but I am wondering how will I pass it back to ParentViewController.
Hope to hear from you soon. Thanks.
Don't think about passing the value you chose back to the cell.detailTextLabel. Instead, think of how to alter your UITableView data source (of the parent's table), and then reload the tableview.
Don't think about passing the value you chose back to the cell.detailTextLabel. Instead, think of how to alter your UITableView data source (of the parent's table), and then reload the tableview.
Ed
Thanks for the reply but how will I alter my UITableView data source? Please advice. Thanks
I have added the codes for your reference and tell me what's wrong since you say I have to focus on ParentController only. ThirdViewController is actually a parentController while Quantity is actually a childController. Thanks for your great help I really appreciate.
tableNum is a name I declare for UITableCell. This code focus on how to redirect to child controller from parent controller.
Code:
// ThirdViewController.m
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
[super loadView];
tableNum = [[UITableView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame] style:UITableViewStyleGrouped];
tableNum.delegate = self;
tableNum.dataSource = self;
tableNum.autoresizesSubviews = YES;
dataArray = [[NSMutableArray alloc] init];
[dataArray addObject: @"Quantity"];
//self.view = tableNum;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0)
{
return [dataArray count];
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.section == 0)
{
NSString *cellValue = [dataArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
cell.textLabel.textColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.detailTextLabel.textColor = [UIColor blueColor];
cell.detailTextLabel.text = @"";
//cell.detailTextLabel.text = [self.countArray objectAtIndex:indexPath.row];
}
return cell;
}
//for selecting which cell that goes to 2nd controller
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.section == 0)
{
if (self.qtyController == nil)
{
Quantity *viewTwo = [[Quantity alloc] initWithNibName:@"Quantity" bundle:[NSBundle mainBundle]];
self.qtyController = viewTwo;
[viewTwo release];
}
qtyController.title = [[NSString alloc] initWithFormat:@"Quantity"];
[self.navigationController pushViewController:self.qtyController animated:YES];
}
Thanks for the reply but how will I alter my UITableView data source? Please advice. Thanks
There are several ways to do this, but perhaps the most straightforward would be to pass a pointer to your parent when instantiating your child (initWithParent:self).
Then, you can send messages to the parent's tableview's datasource. You would need a mutable copy of the array backing the datasource, add whatever you chose in the child, and assigning that as the parent's tableview's datasource, and reloading the tableview of the parent.
First select each row (UITableView) on ParentViewController and then it will push to another controller called ChildViewController which allows the user to select the choices. Upon selecting the choices, how to get this "choices" to be passed back to ParentViewController -> put this data on cell.detailTextLabel.text.
ChildViewController.m (didSelectRowAtIndexPath)
//Obtain the data
NSString *selectString = [countArray objectAtIndex: [indexPath row]];
NSLog(@"The answer is %@", selectString);
* It is able to show the correct value but I am wondering how will I pass it back to ParentViewController.
Hope to hear from you soon. Thanks.
Why is this "How do I do basic communication between view controllers" post in the advanced discussion section? That's a bit like posting a "What colors of paint do I mix to get green" to an advanced oil painting forum.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Why is this "How do I do basic communication between view controllers" post in the advanced discussion section? That's a bit like posting a "What colors of paint do I mix to get green" to an advanced oil painting forum.
Sorry for the late response. My apologies. Well, I have to switch to web application using Dashcode after hearing many feedbacks from my client regarding iPad development. He wants simple User Interface like including radio button or checkbox that allows displaying the list or hidding the list.
By the way, not sure if this forum is considered as Dashcode? In case, I will say it out if you know or not. Do you know why I am unable to view the iPad Simulator after running Dashcode? And do you know where I can enlarge the iPad size instead of iPhone size so I can build more user interface but I understand it is written in Java Script. Not sure if I should switch to Dashcode or Xcode but is it possible to have radio button and checkbox for visible or invisible on Xcode? If yes, I rather use Xcode because I am familar with it.