NotificationCenter 没有在我的 obj c 类( utils 类或对象类)中触发.不是视图控制器 [英] NotificationCenter is not getting trigger in my obj c class ( utills class or object class ). Not an viewcontroller

查看:73
本文介绍了NotificationCenter 没有在我的 obj c 类( utils 类或对象类)中触发.不是视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have one obj c class, swift file. And here is my code :

#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.

userInfo:userInfo is my NSDictionary.

.h 文件代码:

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

现在在我的 swift 文件中:

Now in my swift file :

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 没有打印.不知道这里的问题是什么.任何帮助都会有所帮助.

Now in my obj c class NSLog(@"Finished!"); this is getting print in my console. But openBoard is not printing. Not sure what that the issue here. Any help would be useful.

提前致谢!

更新:

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

When i add breakpoint in my NSNotificationCenter i am getting this warning :

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

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

推荐答案

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

Looking at your sample project, the main problem is that you never instantiate the Swift ViewController class. So, the NotificationCenter.default.addObserver code never runs, and the object doesn't exist to get the notification anyway.

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

In your testLogin.m class, you need to create a ViewController property, and instantiate the class. Make it a property and not a local variable so it doesn't go out of scope.

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

You also need to make a couple changes to your Swift class. Presumably, you intend to load this as a UIViewController, so you could leave the .addObserver line in viewDidLoad(), but... if you haven't yet done anything to actually load the view, that won't be called either. Better to implement the init methods and do it there.

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

In your sample project, replace ViewController.swift with:

//
//  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 替换为:

and replace testLogin.m with this:

//
//  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

只有一些变化——如果你将你的原作与这些进行比较,差异就会很明显.

There are only a few changes -- if you compare your originals with these, the diffs will be obvious.

这篇关于NotificationCenter 没有在我的 obj c 类( utils 类或对象类)中触发.不是视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆