0

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

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112

5 Answers5

1

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 
   }

}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
0

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
    }
LiveBird
  • 114
  • 2
0

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).

Ryan Dignard
  • 639
  • 1
  • 5
  • 16
0
 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
    }
}
Shashi3456643
  • 2,021
  • 17
  • 21
-2

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); 
}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65