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直接或者间接的扯上关系
网友评论