Attempting to segue from Objective-C to Swift VC

后端 未结 2 896
礼貌的吻别
礼貌的吻别 2021-01-25 07:38

i\'m trying to segue from objective-c to swift.

However when I try this using the object below I receive the following error

I don\'t know what i\'m doi

相关标签:
2条回答
  • 2021-01-25 07:49

    I guess the more safe way is to use navigationController?.pushViewController(_:animated:), instead of using segues.

    0 讨论(0)
  • 2021-01-25 07:59

    After discussion in Chat and fixing various issues, I'll take them one by one:

    > *** Assertion failure in -[UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.21.8/UIApplication.m:1707
    > *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'accessing _cachedSystemAnimationFence requires the main thread'
    

    That's the first issue causing a crash. This is talking about an internal method of CocoaTouch needed to be called in main thread.

    The issue lies on your performSegueWithIdentifier:sender:. All UI related calls have to be done in main thread. To fix it:

    dispatch_async(dispatch_get_main_queue(), ^(){
        [self performSegueWithIdentifier:@"brandSegue" sender:self];
    });
    

    Fixing this revealed a second issue:

    it segues but it trigger twice, do you know why this may happen?

    You are doing this:

    for (size_t i = 0; i < trackableCount; i++) 
    {
        if (somethingTest)
        {
            [self performSegueWithIdentifier:@"brandSegue" sender:self];
        }
    }
    

    Who said that in your for loop you don't valid multiple times somethingTest?

    To fix it (I'm talking about the logic, I didn't do the dispatch_async(dispatch_get_main_queue(){()} part to avoid adding noise to the algorithm).

    //Declare a var before the for loop
    BOOL seguedNeedsToBeDone = FALSE;
    for (size_t i = 0; i < trackableCount; i++) 
    {
        if (somethingTest)
        {
            seguedNeedsToBeDone = TRUE;
        }
    }
    //Perform the segue after the for loop if needed
    if (seguedNeedsToBeDone)
    {
        [self performSegueWithIdentifier:@"brandSegue" sender:self];
    }
    

    Next issue, passing data to the Destination ViewController:

    JSONViewController *destViewController = segue.destinationViewController;
    destViewController.brand = @"something";
    

    You are mixing Swift & Objective-C, since XCode was complaining about not knowing brand being a property of JSONViewController object, you needed to add @objc before the declaration of the var. More detailed answer can be found here.

    Finally, a tip to pass the data of you for loop is using the sender (it's faster in term of coding than creating another var, etc.):

    //Calling the performSegue with custom value to pass
    [self performSegueWithIdentifier:@"brandSegue" sender:someVarToSend];
    
    //Passing the custom value
    destViewController.brand = someVarToSend;
    
    0 讨论(0)
提交回复
热议问题