Performing segue from another class

前端 未结 3 792
南方客
南方客 2021-01-16 11:12

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

相关标签:
3条回答
  • 2021-01-16 11:17

    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

    0 讨论(0)
  • 2021-01-16 11:17

    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];
    
    }
    
    0 讨论(0)
  • 2021-01-16 11:18

    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.

    0 讨论(0)
提交回复
热议问题