如何找到我传递给另一个控制器的控制器

时间:2015-02-02 07:13:14

标签: ios objective-c iphone ios7

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

我使用storyBoard ID进行推送。现在我的问题是,如何找到我传递给DeatilViewController

的控制器

5 个答案:

答案 0 :(得分:1)

DetailViewController.h

@property (nonatomic,assign) BOOL comeFromStudentsTable;

StudentsTableViewController.m

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

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

   }
}

TeachersTableViewController.m

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

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

   }
}

DetailViewController.m

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

}

答案 1 :(得分:0)

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

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

@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
    }

答案 2 :(得分:0)

在您的详细信息中,VC写这个以获得推动您的VC。

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 */

据说这是非常脆弱的,你最好在两个VC之间采用通用协议,或者根据你试图显示的对象进行反射检查(即isKindOfClass:...)(因此,不关心你如何得到你在这里如何的语义,而不是 对象让你在这里。)

答案 3 :(得分: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
    }
}

答案 4 :(得分:-2)

谢谢你们。我使用storyBoard ID来了另一个逻辑.. 在" 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];

在" DetailTableViewController.h "

@property(nonatomic,strong)NSString *controllerIdentifier;

在" DetailTableViewController.m "

- (void)viewDidLoad { <br>
    NSLog(@"Passed controller is %@",controllerIdentifier); 
}