简体   繁体   中英

Call a swift method to perform segue from objective c

I'm relatively new to iOS developing and I am facing an issue, I'd like to call a Swift method performing a segue from Objective C

Here is my Swift method :

@objc func callReceived() {
        self.performSegue(withIdentifier: "goto_incomingcall", sender: self)
}

I am calling it from Objective C this way :

ViewController *controller = [ViewController new];
[controller callReceived];

With the new method being so :

func `new`() -> ViewController {
    return ViewController()
}

If I am guessing right, the problem I am facing is that I am initializing a new instance of my class ViewController then calling my method to perform the segue, and as it is not the current instance of the ViewController class, performing the segue from another instance of the class makes the whole program crashes with a signal SIGABRT

What would be the correct way for doing it?

Regards.

Firstly you should import that swift class file in objective c file . After self.performSegue(withIdentifier: "goto_incomingcall", sender: self) you should use - Prepare for segue method for data transfer . Create a reference of that class like this -

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"goto_incomingcall"]){
        Vc = (ViewController *)segue.destinationViewController;
        Vc.MethodSelected();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM