I have two table view controllers StudentsTableViewController
and TeachersTableViewController
which displays students name and teachers name respectively, and I have only one DetailTableViewController
which displays Details of them in sections. I have different sections for these two controllers.
I'm using storyBoard ID for pushing. Now my question is , how to find which controller I'm passing to DeatilViewController

- 23,155
- 15
- 89
- 112

- 833
- 12
- 21
-
Take 2 arrays for Teachers & Students. According to click u have to send a value , according that value you can show the data on TableView. – Soumya Ranjan Feb 02 '15 at 07:15
-
When you are on detailviewcontroller you would like to find out which controller(student or teacher) you came from? – Shashi3456643 Feb 02 '15 at 08:59
-
Thanks @SRNayak.. Why do we need arrays for just knowing which controller im passing ?? – Dayanithi Natarajan Feb 02 '15 at 09:04
-
@SRNayak yes exactly.. – Dayanithi Natarajan Feb 02 '15 at 09:12
-
@Daya Kevin:Its Working or not? – Soumya Ranjan Feb 02 '15 at 09:53
-
Array for keeping data & take a Variable for checking just knowing which controller u came from – Soumya Ranjan Feb 02 '15 at 09:54
-
@SRNayak yes working.. thanks man – Dayanithi Natarajan Feb 02 '15 at 10:04
5 Answers
In your DetailViewController.h
@property (nonatomic,assign) BOOL comeFromStudentsTable;
In your StudentsTableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"segueID_toStudentTable"])
{
DeatilViewController *vc = [segue destinationViewController];
vc.comeFromStudentsTable=YES;
}
}
In your TeachersTableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"segueID_toTeacherTable"])
{
DeatilViewController *vc = [segue destinationViewController];
vc.comeFromStudentsTable=NO;
}
}
In your DetailViewController.m
- (void)viewDidLoad {
if (self.comeFromStudentsTable)
{
// do what you want If come from student
}
else
{
// do what you want If come from teacher
}
}

- 5,663
- 7
- 32
- 65
You can identify it using the kind of class object being passed to the DetailViewController.
For example: Make property of the detail object in DetailViewController
@property (nonatomic,retain) id detailObject; //This will be object of Student/Teacher class.
Now in DetailViewController you can make condition like this:
if([self.detailObject isKindOfClass:[Teacher class]])
{
//Do code for Teacher details
}
else{
//Do code for Student details
}

- 114
- 2
inside your detail VC write this to get the VC that pushed you.
NSArray* navStack = self.navigationController.viewControllers;
UIViewController* parent = navStack[MIN((NSInteger)navStack.count - 2, 0)];
/* -2, -1 because count isn't a valid index, and -1 for the current VC */
That being said this is very brittle, and you are better off adopting a common protocol between the two VCs or have a reflection check (i.e. isKindOfClass:...
) based on the object you're trying to show (and thus not care about how you got about the semantics of how you got here rather what object got you here).

- 639
- 1
- 5
- 16
for(UIViewController *childVC in [self.navigationController viewControllers]){
If([childVC isKindOfClass: [StudentViewController class])
{
//StudentViewController object you can get //from self.navigationalController
}
else If([childVC isKindOfClass: [TeacherViewController class])
{
//TeacherViewController object you can get //from self.navigationalController
}
}

- 2,021
- 17
- 21
-
I haven't compiled the code I just typed on my mobile it should work – Shashi3456643 Feb 02 '15 at 09:24
Thanks guys. I came with another logic using storyBoard ID.. In "StudentTableViewController.m"
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"StudentTableViewController";
detailController.controllerIdentifier = identifier;
[self.navigationController pushViewController:detailController animated:YES];
In "TeacherTableViewController.m"
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"TeacherTableViewController";
detailController.controllerIdentifier = identifier;
[self.navigationController pushViewController:detailController animated:YES];
In "DetailTableViewController.h"
@property(nonatomic,strong)NSString *controllerIdentifier;
In "DetailTableViewController.m"
- (void)viewDidLoad { <br>
NSLog(@"Passed controller is %@",controllerIdentifier);
}

- 5,663
- 7
- 32
- 65

- 833
- 12
- 21