ios - 通知中心未在我的 obj c 类( utills 类或对象类)中触发。不是 View Controller

标签 ios objective-c swift xcode nsnotificationcenter

我有一个 obj c 类,swift 文件。这是我的代码:

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

#include "InterfaceManagerInstance.h"

void IOSInterfaceManager::testMethod() {}
void IOSInterfaceManager::initialize(){
}
std::string IOSInterfaceManager::getColorPrimary(){
    return "";
}


void IOSInterfaceManager::onOver(int nameID,int fameid, int nickNameID){

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"openBoard" object:nil userInfo:userInfo];
            NSLog(@"Finished!");

        });

    }

userInfo:userInfo 是我的 NSDictionary。

.h文件代码:

class IOSInterfaceManager : public InterfaceManager
{
public:
void onOver(int nameID,int fameid, int nickNameID);
};

现在在我的 swift 文件中:

override func viewDidLoad() {
        super.viewDidLoad()
 NotificationCenter.default.addObserver(self, selector: #selector(self.openBoard(notification:)), name: Notification.Name("openBoard"), object: nil)
}

    @objc func openBoard(notification: Notification) {


    }

现在在我的 obj c 类中 NSLog(@"Finished!"); 这正在我的控制台中打印。但是 openBoard 没有打印。不确定这里的问题是什么。任何帮助都会有用。

提前致谢!

更新:

当我在 NSNotificationCenter 中添加断点时,我收到此警告:

warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.

enter image description here

最佳答案

查看您的示例项目,主要问题是您从未实例化 Swift ViewController 类。因此,NotificationCenter.default.addObserver 代码永远不会运行,而且该对象也不存在来获取通知。

在您的 testLogin.m 类中,您需要创建一个 ViewController 属性,并实例化该类。使其成为属性而不是局部变量,这样它就不会超出范围。

您还需要对 Swift 类进行一些更改。据推测,您打算将其加载为 UIViewController,因此您可以.addObserver 行保留在 viewDidLoad() 中>,但是...如果您尚未执行任何操作来实际加载 View ,则也不会被调用。最好实现 init 方法并在那里执行。

在示例项目中,将 ViewController.swift 替换为:

//
//  ViewController.swift
//  notificationCenter
//
//  Created by SATHISH on 3/1/19.
//  Copyright © 2019 sathish. All rights reserved.
//

import UIKit

@objc class ViewController: UIViewController {

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        print("Swift ViewController class init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) was called.")
        setupNotif()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        print("Swift ViewController class init?(coder aDecoder) was called.")
        setupNotif()
    }

    init() {
        super.init(nibName: nil, bundle: nil)
        print("Swift ViewController class init() was called.")
        setupNotif()
    }

    func setupNotif() -> Void {
        NotificationCenter.default.addObserver(self, selector: #selector(self.openScoreBoard(notification:)), name: Notification.Name("openScoreBoard"), object: nil)
    }

    @objc func openScoreBoard(notification: Notification) {
        print("Swift openScoreBoard(notification: Notification) was triggered!")
    }
}

并将 testLogin.m 替换为:

//
//  testLogin.m
//  notificationCenter
//
//  Created by SATHISH on 3/1/19.
//  Copyright © 2019 sathish. All rights reserved.
//

#import "testLogin.h"
#import "notificationCenter-Swift.h"

@interface testLogin ()

@property (strong, nonatomic) ViewController *swiftVC;

@end

@implementation testLogin

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    // instantiate an instance of the Swift ViewController
    _swiftVC = [ViewController new];

}

- (IBAction)clickedAction:(id)sender {

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
         NSDictionary* userInfo = @{@"gameScore": @"123", @"gameID": @"123", @"gameSkillID": @"123"};
        [[NSNotificationCenter defaultCenter] postNotificationName:@"openScoreBoard" object:nil userInfo:userInfo];
        NSLog(@"Finished!");
    });
}

@end

只有一些变化 - 如果您将原始版本与这些版本进行比较,差异就会很明显。

关于ios - 通知中心未在我的 obj c 类( utills 类或对象类)中触发。不是 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54930376/

相关文章:

ios - MPMusicPlayerController 队列问题

ios - 我们如何将具有库的 sdk/Framework 共享给其他项目?

ios - 当我们滑动以退出应用程序时,如果 applicationDidEnterBackground 没有 100% 被调用,我们如何可靠地保存数据?

ios - 即使应用程序被杀死,计划任务

ios - Xcode-9 beta 中的自动布局被破坏

ios - 如何在 Swift 的 UIButton 中删除图像和写入文本?

ios - 在 iOS 中测试 CSS 文件类型的推荐方法?

iOS 保存文件到非私有(private)目录

swift - DispatchQueue下找不到某些实例方法

objective-c - 你如何从 Swift 调用 Objective-C 可变参数方法?