ios - NotificationCenter 在我的 obj c 类(utills 类或对象类)中没有被触发。不是视图控制器
问题描述
我有一个 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);
};
现在在我的快速文件中:
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 类数据。这可能会降低可用类型信息的质量。
解决方案
查看您的示例项目,主要问题是您从未实例化SwiftViewController
类。因此,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
只有一些变化——如果你将你的原件与这些进行比较,差异将是显而易见的。
推荐阅读
- astronomy - 使用 Skyfield 从 TLE 计算卫星远地点和近地点
- android - Livedata在不同的活动中具有不同的价值
- pandas - 为什么我在 Jupyter Notebook 而不是在线 Jupyter 上收到此错误?
- codeblocks - 如何在 code::blocks Projects 选项卡中显示每个文件
- javascript - SuperAgent - 在未经授权时使用新的不记名令牌重试原始请求
- python - Python中的并发图
- python-3.x - 根据特定条件生成带有 ffill 的行 - pandas
- python - 如何访问和过滤具有多个值的字典?
- mysql - Mysql:如果没有更多引用(在几个中),则自动删除行
- python - NameError:名称“pydotplus”未定义