Attempting to segue from Objective-C to Swift VC

烂漫一生 提交于 2019-12-02 03:51:31

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;

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!