如何在objC项目中从ObjC文件导航到swift文件?

时间:2016-05-05 08:59:58

标签: ios objective-c swift

我的项目名称是ObjCSwift。创建项目后,我使用SecondViewController.swift创建了一个新的swift文件xib

我已在#import "ObjCSwift-Swift.h"中导入了AppDelegate.m,这是我ViewController.m的代码

#import "ViewController.h"
#import "ObjCSwift-Swift.h"

@interface ViewController ()
@property (nonatomic, strong) SecondViewController* controller;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)goToSwiftAction:(id)sender {
    _controller = [[SecondViewController alloc] init];
//    _controller = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
_controller.view.backgroundColor = [UIColor purpleColor];
[self.navigationController pushViewController:_controller animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

项目成功运行,没有任何警告。但是当我点击按钮goToSwiftAction时,会调用该方法,但视图不会导航。

我还将嵌入式代码包含Swift代码设置为 YES

我错过了什么?请帮忙。

2 个答案:

答案 0 :(得分:0)

试试这个

从故事板到xib

SecondViewController *viewController=[[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil]; // ensure once your nib name is correctly or not.

[self presentViewController:viewController animated:YES completion:nil];

如果是在NavigatinController中的underwith

[self.navigationController pushViewController:viewController animated:YES];

答案 1 :(得分:0)

这个对我有帮助

将Objective C导航到Swift控制器

关于你的目标C类使用下面的导航到swift控制器

-z

此图片有助于如何在故事板中设置标识符

enter image description here

类似导航Swift到Objective C控制器

YourSwiftViewController *Obj = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"swiftIdentifier"];

[self presentViewController:Obj animated:YES completion:nil];