互联网科技

OC和Swift互相跳转

2017-12-09  本文已影响43人  czj_warrior

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

#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文件的时候回创建一个工程名-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)
        
        
    }

哈哈,大功告成!!!

[图片上传失败...(image-b80a67-1512816958469)]

上一篇下一篇

猜你喜欢

热点阅读