我正在使用JSQMessagesViewController库进行聊天。聊天可能包含网址。如果用户点击网址,则会在Safari中打开网址。
我想在我自己的View Controller中打开具有webview的url。但我不知道如何在swift中制作的目标c中导航视图控制器。
- (BOOL)textView:(UITextView *)textView
shouldInteractWithURL:(NSURL *)URL
inRange:(NSRange)characterRange
interaction:(UITextItemInteraction)interaction
{
// Navigate to next VC
return NO;
}
提前谢谢。
答案 0 :(得分:0)
但我不知道如何在目标c中导航viewcontroller 这是快速制作的。
Use bridging header
使您的swift viewcontroller在objective-c代码中可见,然后您可以使用普通过程推送viewcontroller。
MySwiftVC *vc = [MySwiftVC init....];
vc.someProperty = Blah blah...;
[self.navigationController pushViewController:vc animated:true];
答案 1 :(得分:0)
for framework(e .Cocoapods)
@import frameworkName
项目的(e。手动拖动)
#import "projectName-Swift.h"
并使用它
答案 2 :(得分:0)
如果您想访问Objective-C类,则需要一个Bridging标头,您必须在其中导入Objective-C头文件。
如果你想访问Objective-C中的Swift类,你必须导入swift.h,它是由Xcode自动生成的头文件。 swift.h文件的完整名称是(yourprojectname-Swift.h),例如Example-Swift.h
#import "yourprojectname-Swift.h"
现在您可以在objective-C
中访问ViewController现在,您可以像使用Objective-C
一样使用swiftViewControllers// Name of your Swift View Controller
YourSwiftViewController *yourSwiftVC = [[YourSwiftViewController alloc] init];
[self.navigationController pushViewController:yourSwiftVC animated:YES];
答案 3 :(得分:0)
你需要在Swift和objective-c之间建立桥梁,
在swift类中添加@objc定义如:@objc class Abc{}
。
构建项目,以便Xcode可以自动为您生成桥文件。
在objective-c文件中导入"YourProject-Swift.h"
现在你可以在Objective-C中调用swift类。 希望这会对你有所帮助。
答案 4 :(得分:0)
假设您的swift ViewController有一个URL变量,如:
var url: URL!
使用url 和loadRequest。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
webView.loadRequest(URLRequest(url: url))
}
在Objective-C类
中包含Swift类引用#import "<productName>-Swift.h"
在您的上下文中,Objective-C Controller在处理textView shouldInteractWithURL
时SwiftWebViewController *vc = [[SwiftWebViewController alloc] init];
vc.url = URL;
[self presentViewController:vc animated:YES completion:nil];
return NO;