用Objective-C发送通知并在Swift中接收

3
尝试从Objective-C发送通知并在Swift中接收。 发件人或收件人没有错误,但在目标视图控制器中未调用函数。
视图控制器1:Objective-C
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"notifyme"
     object:self
     userInfo:self.profile];

视图控制器 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")

}

你是指从 Obj-C 发送并在 Swift 中接收吗?哈哈 - Nitin Gohel
我是指从Objective-C代码发送通知,然后尝试从Swift代码接收通知。抱歉打错字了,请检查已粘贴的部分代码。如果需要进一步的信息,请告诉我。感谢您的快速回复。 - Sandeep
1
如果你遇到了任何问题,请在receiveNotification上方添加@objc注释。否则,你的代码看起来很好。 - hris.to
我相信我尝试添加了@objc,但它仍然没有被调用。 - Sandeep
你什么时候发送通知?当 ViewController 1 发送通知时,两个视图控制器是否都已初始化? - Sandeep
我的理解是,如果目标/第二个容器的viewDidLoad()被调用,那么是的。我对Swift中的“视图控制器初始化?”还不熟悉。 - Sandeep
2个回答

3
在Swift3中,我解决了这个问题。
发送OC。
[[NSNotificationCenter defaultCenter] postNotificationName:"You NotificationName" object:nil];

接收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 {

}

2

按照以下步骤进行操作:

首先,在您的第一个视图ViewController.swift中添加以下内容:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "receiveNotification:", name: "notifyme", object: nil)
}

func receiveNotification(ns: NSNotification){
    print("Received Notification")
}

之后在 Obj-c 中添加一个新类,并不要忘记为该类创建桥接头文件。

在桥接头文件中以以下方式导入您的 SecondViewController:

#import "SecondViewController.h"

在您的SecondViewController.m中,当您返回到Swift视图控制器时,请添加以下代码。

- (IBAction)goBack:(id)sender {

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"notifyme"
     object:nil
     userInfo:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
}

欲了解更多信息,请查看示例项目。

更新:

如果第一个控制器是Objective-C,第二个/目标控制器是Swift。

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

@IBAction func goBack(sender: AnyObject) {

    NSNotificationCenter.defaultCenter().postNotificationName("TestNotification", object: nil, userInfo: nil)
    self.dismissViewControllerAnimated(true, completion: nil)
}

Bridging-Header.h

#import "FirstViewController.h"

示例代码


有点困惑。我的第一个控制器是 Objective-C,第二个/目标控制器是 Swift。这个建议仍然适用吗? - Sandeep
抱歉让您感到困惑。第一个控制器是Objective-C,它是通知的发送者。接收者控制器是Swift。不过,您的示例代码非常有帮助。 - Sandeep
你能分享任何样例项目吗? - Dharmesh Kheni
只是更新一下使用情况,我有以下3个视图控制器:
  1. Swift视图控制器
  2. Objc视图控制器(通知发送者)
  3. Swift视图控制器(通知接收者)
- Sandeep
你能帮我理解什么是“视图未创建”,以及如何进行操作吗? - Sandeep
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接