我如何获得segue.destinationViewController isKindOfClass:x,以识别目标类?

时间:2014-08-13 09:56:40

标签: ios objective-c

如果NSLog destinationviewcontroller像这样,我试图收到简单的isKindOfClass消息:

FirstViewController.m

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

    if ([segue.destinationViewController isKindOfClass:[SecondViewController class]]) {

        NSLog(@"The destinationViewController isKindOfClass SecondViewController");
}

在故事板中,我按住Ctrl +从FirstViewController中的条形按钮项目拖动到SecondViewController并选择模态segue。 我还确保#import SecondViewController.h FirstViewContrller.m中的SecondViewController,我已经给了SecondViewController"自定义类"身份检查员中的NSLog

我是否需要添加/执行其他操作,因为我此时未收到{{1}}消息。

编辑:我忘了告知我确实将secondViewController嵌入了navigationViewController中。

3 个答案:

答案 0 :(得分:1)

这应该适合你:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UINavigationController *navigationController = segue.destionationViewController;

    if ([navigationController.viewControllers[0] isKindOfClass:[SecondViewController class]]) {

        NSLog(@"The destinationViewController isKindOfClass SecondViewController");
}

但如果你想要更通用的东西,我会这样做:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    id dvc = segue.destinationViewController;
    if ([dvc isKindOfClass:[UINavigationController class]] && ((UINavigationController *)dvc).viewControllers > 0) {
        dvc = [[dvc viewControllers] firstObject];
    }

    NSLog(@"The destinationViewController is: %@", NSStringFromClass([dvc class]));
}

答案 1 :(得分:0)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{  
    HomeViewController *edit=[segue destinationViewController];  
    if ([HomeViewController.viewControllers[0] isKindOfClass:[CheckViewController class]])       
    {

             //You are ready for coding here

    }

}

试试这段代码。这可能有所帮助。

答案 2 :(得分:-1)

首先,您必须设置destinationViewController

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

             if([segue.identifier isEqualToString:@"YourSegueIdentifierHere"] )
             {
                  //Now you can assign here the destinationViewController
                SecondViewController *secondController = [segue  destinationViewController];

                   //now you don't need this but if you want you can
                  if([segue.destinationViewController isKindOfClass:[SecondViewController class]])

                  {
                         NSLog(@"The destinationViewController isKindOfClass SecondViewController");
                  }




             }

}  

您可以使用[segue.destinationViewController isKindOfClass:[SecondViewController class]]

如果你愿意,可以在if语句中使用。但是现在你不需要从你的xcode中获取标识符。