NotificationCenter没有在我的obj c类(utills类或对象类)中获取触发器。不是viewcontroller

问题描述 投票:0回答:1

我有一个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中添加断点时,我收到此警告:

警告:无法执行支持代码来读取进程中的Objective-C类数据。这可能会降低可用类型信息的质量。

enter image description here

ios objective-c swift xcode nsnotificationcenter
1个回答
0
投票

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

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

您还需要对Swift类进行一些更改。据推测,你打算将它加载为UIViewController,所以你可以在.addObserver中留下viewDidLoad()线,但是......如果你还没有做任何实际加载视图的事情,也不会被调用。更好地实施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

只有一些变化 - 如果你比较你的原件,差异将是显而易见的。

© www.soinside.com 2019 - 2024. All rights reserved.