Ios 如何找到哪个控制器I';我正在传递给另一个控制器

Ios 如何找到哪个控制器I';我正在传递给另一个控制器,ios,objective-c,iphone,ios7,Ios,Objective C,Iphone,Ios7,我有两个表视图控制器StudentsTableViewController和TeachersTableViewController,分别显示学生姓名和教师姓名,我只有一个DetailTableViewController,它在各个部分中显示他们的详细信息。我对这两个控制器有不同的部分我正在使用情节提要ID进行推送。现在我的问题是,如何在您的DetailViewController.h @property (nonatomic,assign) BOOL comeFromStudentsTable;

我有两个表视图控制器
StudentsTableViewController
TeachersTableViewController
,分别显示学生姓名和教师姓名,我只有一个
DetailTableViewController
,它在各个部分中显示他们的详细信息。我对这两个控制器有不同的部分

我正在使用情节提要ID进行推送。现在我的问题是,如何在您的DetailViewController.h

@property (nonatomic,assign) BOOL comeFromStudentsTable;
@property(nonatomic,strong)NSString *controllerIdentifier;
在您的studentstablevewcontroller.m中

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"segueID_toStudentTable"])
   {
       DeatilViewController *vc = [segue destinationViewController];
       vc.comeFromStudentsTable=YES;

   }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"segueID_toTeacherTable"])
   {
       DeatilViewController *vc = [segue destinationViewController];
       vc.comeFromStudentsTable=NO;

   }
}
- (void)viewDidLoad {
   if (self.comeFromStudentsTable)
   {
        // do what you want If come from student 
   }
   else
   {
       // do what you want If come from teacher 
   }

}
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"StudentTableViewController";
detailController.controllerIdentifier = identifier; 
[self.navigationController pushViewController:detailController animated:YES];
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"TeacherTableViewController";
detailController.controllerIdentifier = identifier;
[self.navigationController pushViewController:detailController animated:YES];
- (void)viewDidLoad { <br>
    NSLog(@"Passed controller is %@",controllerIdentifier); 
}
在您的教师StableViewController.m中

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"segueID_toStudentTable"])
   {
       DeatilViewController *vc = [segue destinationViewController];
       vc.comeFromStudentsTable=YES;

   }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"segueID_toTeacherTable"])
   {
       DeatilViewController *vc = [segue destinationViewController];
       vc.comeFromStudentsTable=NO;

   }
}
- (void)viewDidLoad {
   if (self.comeFromStudentsTable)
   {
        // do what you want If come from student 
   }
   else
   {
       // do what you want If come from teacher 
   }

}
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"StudentTableViewController";
detailController.controllerIdentifier = identifier; 
[self.navigationController pushViewController:detailController animated:YES];
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"TeacherTableViewController";
detailController.controllerIdentifier = identifier;
[self.navigationController pushViewController:detailController animated:YES];
- (void)viewDidLoad { <br>
    NSLog(@"Passed controller is %@",controllerIdentifier); 
}

在您的DetailViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"segueID_toStudentTable"])
   {
       DeatilViewController *vc = [segue destinationViewController];
       vc.comeFromStudentsTable=YES;

   }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

if ([[segue identifier] isEqualToString:@"segueID_toTeacherTable"])
   {
       DeatilViewController *vc = [segue destinationViewController];
       vc.comeFromStudentsTable=NO;

   }
}
- (void)viewDidLoad {
   if (self.comeFromStudentsTable)
   {
        // do what you want If come from student 
   }
   else
   {
       // do what you want If come from teacher 
   }

}
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"StudentTableViewController";
detailController.controllerIdentifier = identifier; 
[self.navigationController pushViewController:detailController animated:YES];
DetailTableViewController *detailController;
detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"detailTVC"];
NSString *identifier = @"TeacherTableViewController";
detailController.controllerIdentifier = identifier;
[self.navigationController pushViewController:detailController animated:YES];
- (void)viewDidLoad { <br>
    NSLog(@"Passed controller is %@",controllerIdentifier); 
}

您可以使用传递给DetailViewController的类对象的类型来识别它

例如:在DetailViewController中创建细节对象的属性

@property (nonatomic,retain) id detailObject; //This will be object of Student/Teacher class.
现在在DetailViewController中,您可以创建如下条件:

if([self.detailObject isKindOfClass:[Teacher class]])
    {
        //Do code for Teacher details
    }
    else{
        //Do code for Student details
    }

在你的细节中,写下这篇文章来获得推动你的VC

NSArray*navStack=self.navigationController.viewControllers;
UIViewController*parent=navStack[MIN((NSInteger)navStack.count-2,0)];
/*-2,-1,因为count不是有效的索引,而-1用于当前VC*/


尽管如此,这是非常脆弱的,您最好在两个VCs之间采用一个通用协议,或者根据您试图显示的对象进行反射检查(即,
是类:…
)(因此不关心您是如何得到这里的语义的,而是什么对象得到了这里)。

谢谢大家。我使用故事板ID实现了另一个逻辑。。 在“StudentTableViewController.m

在“TeacherTableViewController.m

在“DetailTableViewController.h

在“DetailTableViewController.m

-(void)viewDidLoad{
NSLog(@“传递的控制器为%@”,controllerIdentifier); }
为教师和学生设置两个数组。根据单击,您必须发送一个值,根据该值,您可以在TableView上显示数据。当您在detailviewcontroller上时,您想知道您来自哪个控制器(学生或教师)?谢谢@SRNayak。。为什么我们需要数组来知道我要通过哪个控制器???@SRNayak是的,完全正确。@Daya凯文:它能不能工作?我还没有编译我刚在手机上输入的代码,它应该能工作