1

I am trying to call performSegueWithIdentifier from different class which is NSObject class and I am receiving this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:

 'Receiver (<DerinlikView: 0x95b1ac0>) has no segue with identifier 'DerinlikToPali''

My code is:

Class1 (NSObject):

    DerinlikView *derinlik = [DerinlikView alloc];
    [derinlik performSegue];

Class2 (UIViewController):

- (void) performSegue
{
    [self performSegueWithIdentifier:@"DerinlikToPali" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ( [[segue identifier] isEqualToString:@"DerinlikToPali"]  )
    {
        Palinolojik *pali = segue.destinationViewController;
    }
}
Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
birdcage
  • 2,638
  • 4
  • 35
  • 58
  • just a thought have you checked the identifier in storyboard and it is the right spelling for your identifier? i had to change my comment since i was wrong in the first section. just check the identifier in story board and see if it is the right one. – Adrian P Mar 18 '13 at 13:41
  • yes Im trying to go from NSObject class to viewcontroller. I tried to place it to NSObject class also but didnt work. I put the segue code as it is to NSObject class. – birdcage Mar 18 '13 at 13:45
  • have you checked the identifier that connects the view controller class to the next view? the segue identifier may be the issue. and also in your segue method Palinolojik is the segue destination but in you ns object class the identifier is related to derinlikview. the destination view should be one of them not both. – Adrian P Mar 18 '13 at 13:46
  • DerinlikView is the name of Class2. I checked the connections. They are fine. If I call from Class2 it works but I need to perform the segue from NSObject class. Actually Im looking for a solution which lets me to perform a segue from NSObject class. I mean from Class2 to Palinolojik. – birdcage Mar 18 '13 at 13:59
  • so you are saying when you call `[self performSegue]` in your Class2, it performs segue but when you call same method from another class such as `[derinlik performSegue]` it says `has no segue with identifier 'DerinlikToPali'` – SpaceDust__ Mar 18 '13 at 14:20
  • Exactly this is true.. – birdcage Mar 18 '13 at 14:32
  • is this a navigation based app ? – SpaceDust__ Mar 18 '13 at 14:35
  • yes Im using storyboard and "modal" segues for navigation. – birdcage Mar 18 '13 at 14:38
  • try to update your question with a screen shot of your storyboard – SpaceDust__ Mar 18 '13 at 15:02
  • here you can find the sample project which demonstrates my problem [link](http://bit.ly/XUqQcU) – birdcage Mar 18 '13 at 15:21

3 Answers3

7

I was having the same issue and I managed to resolve it by using NSNotificationCenter.

In the NSObject class .m file I added:

[[NSNotificationCenter defaultCenter] postNotificationName:@"performsegue"
                                                    object:nil];

In the ViewController class .m file -

1.In the -(void) viewDidLoad I added:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(performsegueNotification:)
                                             name:@"performsegue"
                                           object:nil];

2.In the -(void) viewDidUnload I added:

[[NSNotificationCenter defaultCenter] removeObserver:self];

3.Created the method performsegueNotification:

-(void)performsegueNotification:(NSNotification *) notif
{

    [self performSegueWithIdentifier: @"PresentView" sender:self];

}
user2327003
  • 103
  • 5
2

I believe nothing wrong with your storyboard or your segues , what not.

Problem must be this line=

DerinlikView *derinlik = [DerinlikView alloc];

When you allocate a new viewcontroller pointer for your Class2 it cant find the segue connected to your view.

A solution would be to pass the pointer of your initiated Class2 to NSObject class.

in your class1.h:

@interface Class1 : NSObject
{
    Class2 *viewController;
}

@property (nonatomic,strong) Class2 *viewController;

in class1.m

@synthesize viewController;

[self.viewController performSegue];

In class2.m:

Class1 *callNsObject= [[Class1 alloc] init];
UIViewController *currentVC=self;//this is the part you need the pass current viewcontrollers pointer to your nsobject class depends on your project thee are multiple ways of doing it.
callNsObject.viewController=currentVC;

Important : I dont know hierarchy of your Class2 viewcontroller so in the line UIViewController *currentVC=self; you have to change self to your current view controller pointer.

For example If it is a navigation based app, you can get the current view controller by, UIViewController *currentVC = self.navigationController.visibleViewController;

Also I am assuming that your Class2's view is pushed to stack before you call your Class1 method

SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
  • It didnt trigger the performSegue method in Class2. where should I place this code --> Class1 *callNsObject= [[Class1 alloc] init]; UIViewController *currentVC=self;//this is the part you need the pass current viewcontrollers pointer to your nsobject class depends on your project thee are multiple ways of doing it. callNsObject.viewController=currentVC; – birdcage Mar 18 '13 at 15:04
  • it is an example when you call your Class1 (NSObject) pass your Class2 's current viewcontroller pointer to your Class1. You need to place it before you call your Class1 methods – SpaceDust__ Mar 18 '13 at 15:21
0

The error note says that "DerinlikToPali" was not found in StoryBoard.

Go to your storyBoard, select the segue and check it's Attribute Inspector (menu View - Utilities - Show Attribute Inspector). Make sure the identifier is the same string you use at performSegueWithIdentifier.

Update (after looking at SampleProject):

  • SampleClass *sample = [SampleClass alloc] should be "alloc init" in general, so you not only allocate memory, but also complete the full object initialization.
  • SampleClass methodToSegue() doesn't create ViewController object as you expected. First see above "alloc init", but in this case you need more (below). Otherwise you will NOT have a valid UIViewController object. Note that you got to first give it a name at storyBoard, so that you can create it (using that name). Alternative is to use XIB instead of storyBoard, in which case you use initWithNibName:bundle:

    ViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil] instantiateViewControllerWithIdentifier:@"viewController"];

  • Biggest problem is logical and I hope it's only within this SampleProject: objectA is created, creates objectB, which creates objectA and calls it's method... except that objectA creation creates objectB again --> and we have a forever loop and something will crash.

Anyway, my updated answer is that your object creation was not completed before you tried to trigger the segue. Especially "ViewController" was not yet an actual UIViewController defined at storyBoard and thus did not have the expected segue.

JOM
  • 8,139
  • 6
  • 78
  • 111
  • I know what u mean but its there. When I call it from Class2 it works. But im trying to call same segue from different class. This is my problem.. – birdcage Mar 18 '13 at 14:31