0

I'm trying to get a simple NSLog message if the destinationviewcontroller isKindOfClass like this:

FirstViewController.m

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

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

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

Within the storyboard i have ctrl+dragged from a bar button item in the FirstViewController to the SecondViewController and chosen modal segue. I've also made sure to #import the SecondViewController.h in FirstViewContrller.m and i have given the SecondViewController the "custom class" of SecondViewController in the identity inspector.

Do i need to add/do something else, because I'm not getting the NSLog message at this point.

Edit: I forgot to inform that i did in fact embed the secondViewController in a navigationViewController.

XT_Nova
  • 1,110
  • 5
  • 17
  • 32
  • Try logging destinationViewController - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSLog(@“%@“,segue.destinationViewController ); } – Rajesh Aug 13 '14 at 10:05
  • Hm, does one really know the class of the destination segue controller at that point at all? Isn't that per se a NSViewController and usually is cast to the actual controller? – Volker Aug 13 '14 at 10:08
  • Are you sure you weren't embed your ViewController in Navigation controller? – Szu Aug 13 '14 at 10:12
  • @Szu You are right! I did embed it in a navigation controller. Guessing that's why it won't work then? But why? – XT_Nova Aug 13 '14 at 10:19

3 Answers3

1

This should work for you:

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

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

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

But if you want something more generic I would do:

- (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]));
}
Szu
  • 2,254
  • 1
  • 21
  • 37
  • Since i had my view controller embedded in an Navigation Controller, this works like a charm. I tried my code without embedding the view controller in an navigation controller and then the NSLog message got through. Thank you Szu. – XT_Nova Aug 13 '14 at 10:34
  • @getJETsetTER Please look at my updated answer it might be interesting for you. – Szu Aug 13 '14 at 10:35
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

    }

}

Try this code. This might help.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34
Monikanta
  • 307
  • 2
  • 8
-1

First you would have to set 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");
                  }




             }

}  

You can use [segue.destinationViewController isKindOfClass:[SecondViewController class]]

inside the if statement if you want.But its not needed now you can get the Identifier from your xcode.

Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22
  • 1
    You shouldn't compare strings using == operator. Please look at: http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison – Szu Aug 13 '14 at 11:02