美文网首页互联网科技
OC和Swift互相跳转

OC和Swift互相跳转

作者: czj_warrior | 来源:发表于2017-12-09 18:45 被阅读43次

    首先在需要引入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)
            
            
        }
    

    哈哈,大功告成!!!

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

    相关文章

      网友评论

        本文标题:OC和Swift互相跳转

        本文链接:https://www.haomeiwen.com/subject/qidpixtx.html