OC和Swift互相跳转

欢迎访问我的个人博客

首先在需要引入Swift的文件中导入头文件#import "工程名-Swift.h"

  • OC跳转Swift页面:
#import "ViewController.h"
#import "OCAndSwift-Swift.h"        // 引入头文件

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)btnClicked:(id)sender {
    
    // Swift文件
    ChildViewController *vc = [[ChildViewController alloc] initWithNibName:@"ChildViewController" bundle:nil];
    
    [self.navigationController pushViewController:vc animated:YES];
    
}
  • Swift跳转OC页面:

首先创建Swift文件的时候回创建一个工程名-Bridging-Header.h文件,在这个文件中将需要用到的OC文件导入到这里!!!

import UIKit

class ChildViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func btnClicked(_ sender: Any) {
        let twoVC = ChildTwoViewController(nibName:"ChildTwoViewController", bundle: nil)
        
        self.navigationController?.pushViewController(twoVC, animated: true)
        
        
    }

哈哈,大功告成!!!

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦