Two view controllers use same table view - ios

I'm new to iOS development. I want to know how to pass back data from same tableview to two different view controller.
This is my didselectrow method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendarea = [tableData objectAtIndex:indexPath.row];
Step2ClassifiedViewController *parentViewC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
parentViewC.areareceive = sendarea;
[self.navigationController popViewControllerAnimated:YES];
}
But this is only for one view. I have another view also need to pass back the data if i use the same table. Is that possible? or do i just need to create another table?.

You can use same table. Only you need change the data source. And can call in each didSelectRowAtIndexpath
This is just like, A taxi driver carries passenger by his one taxi. When his taxi is empty,he can carries passenger.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
sendarea = [tableData objectAtIndex:indexPath.row];
if(state == 1){
Step2ClassifiedViewController *parentViewC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];
parentViewC.areareceive = sendarea;
[self.navigationController popViewControllerAnimated:YES];
}
else if (state == 2){
//get your other View Controller and set data before pop to that view controller.
NSArray *viewControllers = [[self navigationController] viewControllers];
for(UIViewController *vc in viewControllers){
if([vc isKindOfClass:[OtherViewController class]]){
OtherViewController *otherVC = (OtherViewController*)vc;
otherVC.areareceive = sendarea;
[[self navigationController] popToViewController:otherVC animated:YES];
}
}
}
else{
//load any View Controller you want
}
}
Based on your question and the comments, I assume that you want to load different View Controllers based on your different state and pass along data.
The above is one way of doing that. However, you could also use delegation for your purpose, it would be a good programming practise.

Related

How to pass an object by reference from one class to other class?

I an initialising a new view controller by tapping the cell of current controller. All I want to pass the object at cell to next controller by reference so that if I change the object in new controller it should change at original place.
I have tried much to get answer about it but could not get anywhere.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
RecentItemDetailViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"RecentItemDetailViewController"];
vc.station = self.stations[indexPath.row];
[self.navigationController pushViewController:vc animated:YES];
}
All I want to get self.stations[indexPath.row] object in vc.station by reference so if I change vc.station then self.stations[indexPath.row] should also be changed.
self.stations and vc.station both are NSMutableArray type.
You already did it correctly. What you are most likely missing is reloading table view when you come back.
You can override view will appear method. Then check what is the selected row on your table view. Reload that row.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(self.tableView.indexPathForSelectedRow) {
[self.tableView reloadRowsAtIndexPaths:#[tableView.indexPathForSelectedRow] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// Or just reload the whole thing
[self.tableView reloadData];
}
But this naturally assumes you are never ever assigning to station property inside the RecentItemDetailViewController. You can call things like [self.station add...] but never self.station = ....

Navigating pages in different tableview

I have created a screen containing two table views where i have successfully called the method cellForRowAtIndexPath but now am facing trouble in navigating to the other page from tableview option by using didSelectRowAtIndexPath.I think am doing some mistake and i have clearly no idea what is the next step.Can anyone guide how should i do it?? I have used this code-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([tableView isEqual:leftTableView]) {
TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooSinglesScreen"];
EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:#"EyecatcherScreen"];
TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooToplist"];
int i=indexPath.row;
if(i==0){
[self.navigationController pushViewController:obj animated:nil];
}
else if (i==1) {
[self.navigationController pushViewController:obj1 animated:NO];
}
else if (i==2) {
[self.navigationController pushViewController:obj2 animated:NO];
}
}
else {
TattooSinglesScreen *obj=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooSinglesScreen"];
EyecatcherScreen *obj1=[self.storyboard instantiateViewControllerWithIdentifier:#"EyecatcherScreen"];
TattooToplist *obj2=[self.storyboard instantiateViewControllerWithIdentifier:#"TattooToplist"];
int i=indexPath.row;
if(i==0){
[self.navigationController pushViewController:obj animated:nil];
}
else if (i==1) {
[self.navigationController pushViewController:obj1 animated:NO];
}
else if (i==2) {
[self.navigationController pushViewController:obj2 animated:NO];
}
}
}
If you want to navigate from table view to another view controller don't set animated:NO.set animated:YES for navigating.But if you set animated:NO,definitely it does not navigate.So you have to do following things for navigating.
YourViewController *yourVC = (YourViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"StoryBordIdentifier"];
[self.navigationController pushViewController:yourVC animated:YES];
Also You can use in this didSelectRowAtIndexPath
[self performSegueWithIdentifier:#"segueID" sender:self];
with
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
YourViewController *vc = segue.destinationViewController;
}
Click Table Row and Go another Page
Segue or didSelectRowAtIndexPath
Making Push Segue when table row is selected
I see user3182143's answer helped, but for what it's worth I've set up a tiny demo project (now updated with two tables) to show anybody interested how it's done.
It also illustrates (as the name implies), that you can, indeed, push manually and set the animation parameter to NO, just run it in the simulator.
I can't tell why that didn't work in your original approach, pri, but my guess is that you set up your navigation view controller wrongly somehow.
For code & storyboard readability I'd recommend using segues anyways.

didSelectRowAtIndexPath or prepareForSegue to use?

i want to be able to select a item in my tableview and get to the correct view.
say i have two items in my uitableview. one has a value of ubuntu other has a value of arch- in my plist from arraA i have a SystemID which holds the text Ubuntu/Arch, never both
now when i select the one with ubuntu i want it to push to a new view where ubuntu stuff is
and if i go back and select arch it will again push me to a view where arch stuff is
so far i made this and it's not working as intended:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *segueName = nil;
if ([[arrayA valueForKey:#"SystemID"]isEqualToString:#"Ubuntu Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Ubuntu Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
else if ([[arrayA valueForKey:#"SystemID"]isEqualToString:#"Arch Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Arch Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
[self performSegueWithIdentifier: segueName sender: self];
}
also tried prepare forsegue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([arrayA valueForKey:#"Arch Linux"])
{
ActionViewController *controller = (ActionViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
NSLog(#"hostname = %#", controller.Serverinfo);
}
if ([arrayA valueForKey:#"Ubuntu Linux"]) {
ActionViewController *controller = (ActionViewController *)segue.destinationViewController;
NSIndexPath *indexPath = [self.mainTableView indexPathForSelectedRow];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
NSLog(#"hostname = %#", controller.Serverinfo);
}
}
arrayA is a NSMutableArray that read from a plist that you can see here
Replace your method to:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([arrayA[indexPath.row][#"SystemID"]isEqualToString:#"Ubuntu Linux"]) {
//ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Ubuntu Linux" bundle:nil];
// Make sure you replace Storyboard with your storyboard name
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
ActionViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"Ubuntu Linux"];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
else if ([arrayA[indexPath.row][#"SystemID"]isEqualToString:#"Arch Linux"]) {
ActionViewController *controller = [[ActionViewController alloc] initWithNibName:#"Arch Linux" bundle:nil];
controller.Serverinfo = [arrayA objectAtIndex:indexPath.row];
[self.navigationController pushViewController:controller animated:YES];
}
}
You don't need to call [self performSegueWithIdentifier: segueName sender: self]; in didSelectRow because you already push your view in didSelectRow method. If you call it you puts the view twice.
Ooh boy, you're off in the weeds.
First, stop talking about pushing a "view." The term view and view controller are NOT interchangeable. A view is an object that appears on the screen. A view CONTROLLER is an object that manages a collection of views (usually the entire screen.)
You push a view controller, not a view. The view controller then displays it's content view on the screen.
Now, as to your code.
The if statement in your tableView:didSelectRowAtIndexPath: method is messed up.
if ([[arrayA valueForKey:#"SystemID"]isEqualToString:#"Ubuntu Linux"])
You don't want to use valueForeKey on an array. That method attempts to call valueForKey on every object in your array, and what it returns is an array of the results.
Arrays are indexed by a numeric index, not a key. You want an array as the data source for a table view.
What you want to do is to fetch the object at your selected row from your array.
In order to help you further, though, you need to explain what you have stored in your arrayA. Is it an array of dictionaries? An array of strings?
Also, you are mixing NIB based view controller code with storyboard code. You can certainly use both in the same program if you need to, but as a beginner you probably should not.
In your tableView:didSelectRowAtIndexPath method you're creating a view controller with initWithNibName, pushing it, and then calling performSegueWithIdentifier. You want to do one or the other, not both.
Use storyboard with prepareForSegue

IOS - get a value from a table view controller and send it to another table view controller

I have two Table View Controller in a Navigation Controller. The Cloth table is the main view and when I click on one of the cells go to another view controller that gives me the detail, when I select the option i want and return to the Return of the Navigation button, I lose the information. How do I take the value to the main view?
Here my code: Main View - AddClothViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[list objectAtIndex:indexPath.row] isEqual:#"Categorias"] )
{
CategoriesViewController *DVC = [[CategoriesViewController alloc] initWithNibName:#"CategoriesViewController" bundle:nil];
[self.navigationController pushViewController:DVC animated:YES];
}
}
Here my code: Categories ViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set the checkmark accessory for the selected row.
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
// This is my main View Controller when i declare a variable category and I try to access from
//here
AddClothViewController *DVC = [[AddClothViewController alloc] initWithNibName:#"AddClothViewController" bundle:nil];
DVC.clothNew.categoria = [list objectAtIndex:indexPath.row ];
}
I resolved my problem by using this tutorial.
(Simple delegate tutorial for ios development)

Different custom View controller for iOS splitViewcontroller

I have a splitview controller containing master and detailview controller. But now i need to display different custom view controller based on the selection in the masterviewcontroller (a tableview). How can i achieve this?
Write below code in master view controller.
Take the decision of creating custom view based on indexpath value.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UISplitViewController *splitController = self.splitViewController;
NSMutableArray *arr = [splitController.viewControllers mutableCopy];
CustomViewController *customVC = [[CustomViewController alloc]init];
[arr replaceObjectAtIndex:1 withObject:customVC];
[customVC release];
splitController.viewControllers = arr;
}

Resources