OC調用Swift

####OC項目中調用Swift
方法很簡單但是網上資料比較亂,這邊稍微整理一下

#import “ProjectName-Swift.h”

在需要調用的文件中導入以上頭文件,格式爲項目名稱+Swift.h

#import "BUICardDemo-Swift.h"//工程名爲BUICardDemo
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, 100, 50)];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor redColor];
    [self.view addSubview:btn];
    
    
}

-(void)btnClick
{
//swift VC OC中調用
    CusViewController *cusVC = [[CusViewController alloc]init];
    [self presentViewController:cusVC animated:true completion:nil];
}


@end

另外多一句就是swift想要在OC中調用就必須跟NSObject直接或者間接的扯上關係

發佈了123 篇原創文章 · 獲贊 6 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章