Simple OC controller and Swift controller click jump

Create a demo

1. Create a demo project named OCSwift and initialize the selected language to be OC

2. I divided two groups, one OC and one Swift, to establish controllers respectively

Ocswift-bridging -Header ocswift-bridging -Header ocswift-bridging -Header ocswift-bridging -Header ocswift-bridging – OC

#import "OCTestViewController.h"
Copy the code

4. Add a navigation bar to main. storyboard. Note the change of the arrow, remove the default controller of navigation and set the original ViewController to rootViewController with a button

Add a button to this Swift controller as well

Start operation

1.ViewController->SwiftTestVC

  • #import “ocswift-swift. h” to import the header file xxx-swift. h XXX is the name of the project

    • Now I’ve changed this and OCSwiftHH says it’s missing

  • I will change this so that there is no error and it will work, so it is better not to write Chinese or special symbols (not tested)

  • Simple jump push past

    - (IBAction)pushAction:(id)sender {
//    Swift1VC *swiftVC = [[Swift1VC alloc]init];
//    [self.navigationController pushViewController:swiftVC animated:YES];
    SwiftTestVC *swiftVC = [[SwiftTestVC alloc]init];
    [self.navigationController pushViewController:swiftVC animated:YES];
}
Copy the code
  • 2.SwiftTestVC->OCTestViewController
    • #import “octestViewController.h”
    • Push and you’re done
@IBAction func push(_ sender: Any) { let ocTestVC = OCTestViewController() self.navigationController? .pushViewController(ocTestVC, animated: true) }Copy the code

3.OC calls Swift class attribute or object method static method to add the following keyword

@objcMembers @objc

4.Swift calls the OC class attribute or object method static method to write the method to the.h file

5. We didn’t have to create it ourselves

If you go in there and look at it, you’ll find that there are classes for the two controllers that we’ve written for Swift and I think the compiler has already done that for us because it’s got all the methods that it calls in Swift

What if the bridge file was not created in the first place

1. Select the project and go to New File ->iOS-> Header File. 2. Click Next to name the bridge file. 3. Click on the Create; 4. In Build Setting -> Objective-c Bridging Header, add the Bridging file paths in the format of $(SRCROOT)/Bridging- header.h

If you create a folder in the project, write the folder name in the middle, for example, $(SRCROOT)/ folder name/bridge-header. h;

5. If the path is not filled in correctly, the compiler will report an error!! Use #import to import the OC header from the bridge file.