[英]send notification from objectivec and receive in swift
Trying to send notification from objective c and receive in swift. 尝试从目标c发送通知并在swift中接收。 I see no error from sender or receiver and function is not called in target view controller
我看到发送者或接收者没有错误,并且在目标视图控制器中没有调用函数
ViewController 1: Objective c ViewController 1:目标c
[[NSNotificationCenter defaultCenter]
postNotificationName:@"notifyme"
object:self
userInfo:self.profile];
ViewController 2: swift ViewController 2:swift
override func viewDidLoad() {
super.viewDidLoad()
print("view did load")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "notifyme", object: nil)
}
func receiveNotification(ns: NSNotification){
print("Received Notification")
}
In Swift3, I Solved this problem 在Swift3中,我解决了这个问题
Send OC 发送OC
[[NSNotificationCenter defaultCenter] postNotificationName:"You NotificationName" object:nil];
Receive Swift 3 收到Swift 3
func applicationDidBecomeActive(_ application: UIApplication) {
NotificationCenter.default.addObserver(self, selector: #selector(self.handleNotification), name: NSNotification.Name.init(rawValue: "You NotificationName"), object: nil)
}
func handleNotification() -> Void {
}
Follow this steps: 请遵循以下步骤:
First Of all add this in your ViewController.swift which is your first view: 首先在你的ViewController.swift中添加它,这是你的第一个视图:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "notifyme", object: nil)
}
func receiveNotification(ns: NSNotification){
print("Received Notification")
}
After that add a new class in Obj-c and don't forget to create bridging header for that class. 之后在Obj-c中添加一个新类,不要忘记为该类创建桥接头。
In bridging header file add import your SecondViewController this way: 在桥接头文件中添加以这种方式导入您的SecondViewController:
#import "SecondViewController.h"
And in your SecondViewController.m
add this code when you go back to your swift viewController. 在你的
SecondViewController.m
当你回到swift viewController时添加这段代码。
- (IBAction)goBack:(id)sender {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"notifyme"
object:nil
userInfo:nil];
[self dismissViewControllerAnimated:NO completion:nil];
}
For more Info check THIS sample project. 有关更多信息,请查看此示例项目。
Update: 更新:
If first controller is objective c and second/target controller is swift. 如果第一个控制器是目标c而第二个/目标控制器是快速的。
FirstViewController.m FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
}
@end
SecondViewController.swift SecondViewController.swift
@IBAction func goBack(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("TestNotification", object: nil, userInfo: nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
Bridging-Header.h 转职Header.h
#import "FirstViewController.h"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.